home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / varie / uae-0_64.lha / uae-0.6.4 / src / custom.c < prev    next >
C/C++ Source or Header  |  1996-09-10  |  84KB  |  3,055 lines

  1.  /*
  2.   * UAE - The Un*x Amiga Emulator
  3.   * 
  4.   * Custom chip emulation
  5.   * 
  6.   * (c) 1995 Bernd Schmidt, Alessandro Bissacco
  7.   */
  8.  
  9. #include "sysconfig.h"
  10. #include "sysdeps.h"
  11.  
  12. #include <ctype.h>
  13. #include <assert.h>
  14.  
  15. #include "config.h"
  16. #include "options.h"
  17. #include "events.h"
  18. #include "memory.h"
  19. #include "custom.h"
  20. #include "newcpu.h"
  21. #include "cia.h"
  22. #include "disk.h"
  23. #include "blitter.h"
  24. #include "xwin.h"
  25. #include "os.h"
  26. #include "keybuf.h"
  27. #include "serial.h"
  28.  
  29. /* #define EMULATE_AGA */
  30.  
  31. #ifndef EMULATE_AGA
  32. #define AGA_CHIPSET 0
  33. #else
  34. #define AGA_CHIPSET 1
  35. #endif
  36.  
  37. #define SMART_UPDATE 1
  38.  
  39. #define MAX_PLANES 8
  40.  
  41. #define PIXEL_XPOS(HPOS) (((HPOS)*2 - 0x30)*(use_lores ? 1 : 2))
  42.  
  43. /* These are default values for mouse calibration.
  44.  * The first two are default values for mstepx and mstepy.
  45.  * The second line set the orizontal and vertical offset for amiga and X 
  46.  * pointer matching
  47.  */
  48.    
  49. #define defstepx (1<<16)
  50. #define defstepy (1<<16)
  51. #define defxoffs 0
  52. #define defyoffs 0
  53.     
  54. /* Values below define mouse auto calibration process.
  55.  * They are not critical, change them if you want.
  56.  * The most important is calweight, which sets mouse adjustement rate */ 
  57.  
  58. static const int docal = 60, xcaloff = 40, ycaloff = 20;
  59. static const int calweight = 3;
  60. static int lastsampledmx, lastsampledmy;
  61.  
  62.  /*
  63.   * Events
  64.   */
  65.  
  66. unsigned long int cycles, nextevent;
  67. int vpos;
  68. UWORD lof;
  69.  
  70. struct ev eventtab[ev_max];
  71.  
  72. int copper_active;
  73.  
  74. static const int dskdelay = 2; /* FIXME: ??? */
  75.  
  76.  /* 
  77.   * hardware register values that are visible/can be written to by someone
  78.   */
  79.  
  80. static int fmode;
  81.  
  82. static UWORD cregs[256];
  83.  
  84. UWORD intena,intreq;
  85. UWORD dmacon;
  86. UWORD adkcon; /* used by audio code */
  87.  
  88. static ULONG cop1lc,cop2lc,copcon;
  89.  
  90. /* Kludge. FIXME: How does sprite restart after vsync work? */
  91. static int spron[8];
  92. static CPTR sprpt[8];
  93.  
  94. static ULONG bpl1dat,bpl2dat,bpl3dat,bpl4dat,bpl5dat,bpl6dat,bpl7dat,bpl8dat;
  95. static WORD  bpl1mod,bpl2mod;
  96.  
  97. xcolnr acolors[64];
  98.  
  99. UBYTE *r_bplpt[MAX_PLANES];
  100. static CPTR bplpt[MAX_PLANES];
  101.  
  102. /*static int blitcount[256];  blitter debug */
  103.  
  104. struct bplinfo {
  105. #if AGA_CHIPSET == 0
  106.     /* X86.S will break if this isn't at the beginning of the structure. */
  107.     UWORD color_regs[32];
  108. #else
  109.     ULONG color_regs[256];
  110. #endif
  111.     UWORD bplcon0,bplcon1,bplcon2,bplcon3,bplcon4;
  112.     UWORD diwstrt,diwstop,ddfstrt,ddfstop;
  113.  
  114.     UWORD sprdata[8], sprdatb[8], sprctl[8], sprpos[8];
  115.     int sprarmed[8];
  116. } bpl_info;
  117.  
  118. struct line_description
  119. {
  120.     int inborder;
  121.     xcolnr bordercol;
  122.     struct bplinfo bpl_info;
  123.     struct mem_notify_node *mnn;
  124.     CPTR bplpt[MAX_PLANES];
  125.     int linedata_valid;
  126. };
  127.  
  128. static int frame_redraw_necessary;
  129.  
  130. /* 50 words give you 800 horizontal pixels. An A500 can't do that, so it ought
  131.  * to be enough. */
  132. #define MAX_WORDS_PER_LINE 50
  133. static UBYTE line_data[numscrlines * 2][MAX_PLANES][MAX_WORDS_PER_LINE*2];
  134. static struct line_description linedescr[numscrlines * 2];
  135.  
  136. static ULONG dskpt;
  137. static UWORD dsklen,dsksync;
  138.  
  139. static int joy0x, joy1x, joy0y, joy1y;
  140. int joy0button;
  141. UWORD joy0dir;
  142. static int lastspr0x,lastspr0y,lastdiffx,lastdiffy,spr0pos,spr0ctl;
  143. static int mstepx,mstepy,xoffs=defxoffs,yoffs=defyoffs;
  144. static int sprvbfl;
  145.  
  146. static enum { normal_mouse, dont_care_mouse, follow_mouse } mousestate;
  147.  
  148.  /*
  149.   * "hidden" hardware registers
  150.   */
  151.  
  152. int dblpf_ind1[256], dblpf_ind2[256], dblpf_2nd1[256], dblpf_2nd2[256];
  153. int dblpf_aga1[256], dblpf_aga2[256], linear_map_256[256], lots_of_twos[256];
  154.  
  155. int dblpfofs[] = { 0, 2, 4, 8, 16, 32, 64, 128 };
  156.  
  157. static ULONG coplc;
  158. static UWORD copi1,copi2;
  159.  
  160. static enum {
  161.     COP_stop, COP_read, COP_wait, COP_move, COP_skip
  162. } copstate;
  163.  
  164. static int dsklength;
  165.  
  166. int plffirstline,plflastline,plfstrt,plfstop,plflinelen;
  167. int diwfirstword,diwlastword;
  168. int plfpri[3];
  169.  
  170. int max_diwstop, prev_max_diwstop;
  171.  
  172. int dskdmaen; /* used in cia.c */
  173.  
  174. int bpldelay1, bpldelay2;
  175. int bplehb, bplham, bpldualpf, bpldualpfpri, bplplanecnt, bplhires;
  176.  
  177. static int pfield_fullline,pfield_linedone;
  178. static int pfield_linedmaon;
  179. static int pfield_lastpart_hpos,last_sprite;
  180. static int slowline_nextpos, slowline_linepos, slowline_lasttoscr;
  181.  
  182. union {
  183.     /* Let's try to align this thing. */
  184.     double uupzuq;
  185.     long int cruxmedo;
  186.     unsigned char apixels[1000];
  187. } pixdata;
  188.  
  189. char spixels[1000]; /* for sprites */
  190. char spixstate[1000]; /* more sprites */
  191.  
  192. ULONG ham_linebuf[1000];
  193. ULONG aga_linebuf[1000], *aga_lbufptr;
  194.  
  195. char *xlinebuffer;
  196. int next_lineno, linetoscreen, line_in_border;
  197.  
  198. /*
  199.  * Statistics
  200.  */
  201.  
  202. static unsigned long int msecs = 0, frametime = 0, timeframes = 0;
  203. static unsigned long int seconds_base;
  204. int bogusframe;
  205.  
  206. /*
  207.  * helper functions
  208.  */
  209.  
  210. static void pfield_doline_slow(int);
  211. static void pfield_doline(void);
  212. static void do_sprites(int, int);
  213.  
  214. int inhibit_frame;
  215. static int framecnt = 0;
  216.  
  217. static __inline__ void count_frame(void)
  218. {
  219.     if (inhibit_frame) 
  220.         framecnt = 1;
  221.     else {
  222.     framecnt++;
  223.     if (framecnt >= framerate)
  224.             framecnt = 0;
  225.     }
  226. }
  227.  
  228. static __inline__ void setclr(UWORD *p, UWORD val)
  229. {
  230.     if (val & 0x8000) {
  231.     *p |= val & 0x7FFF;
  232.     } else {
  233.     *p &= ~val;
  234.     }
  235. }
  236.  
  237. static __inline__ int current_hpos(void)
  238. {
  239.     return cycles - eventtab[ev_hsync].oldcycles;
  240. }
  241.  
  242. static void calcdiw(void)
  243. {
  244.     if (use_lores) {
  245.     diwfirstword = (bpl_info.diwstrt & 0xFF) - 0x30 - 1;
  246.     diwlastword  = (bpl_info.diwstop & 0xFF) + 0x100 - 0x30 - 1;
  247.     } else {
  248.     diwfirstword = (bpl_info.diwstrt & 0xFF) * 2 - 0x60 - 2;
  249.     diwlastword  = (bpl_info.diwstop & 0xFF) * 2 + 0x200 - 0x60 - 2;
  250.     }
  251.     if (diwfirstword < 0) diwfirstword = 0;
  252.     if (diwlastword > max_diwstop) max_diwstop = diwlastword;
  253.     
  254.     plffirstline = bpl_info.diwstrt >> 8;
  255.     plflastline = bpl_info.diwstop >> 8;
  256. #if 0
  257.     /* This happens far too often. */
  258.     if (plffirstline < minfirstline) {
  259.     fprintf(stderr, "Warning: Playfield begins before line %d!\n", minfirstline);
  260.     plffirstline = minfirstline;
  261.     }
  262. #endif
  263.     if ((plflastline & 0x80) == 0) plflastline |= 0x100;
  264. #if 0 /* Turrican does this */
  265.     if (plflastline > 313) {
  266.     fprintf(stderr, "Warning: Playfield out of range!\n");
  267.     plflastline = 313;
  268.     }
  269. #endif
  270.     plfstrt = bpl_info.ddfstrt;
  271.     plfstop = bpl_info.ddfstop;
  272.     if (plfstrt < 0x18) plfstrt = 0x18;
  273.     if (plfstop > 0xD8) plfstop = 0xD8;
  274.     if (plfstrt > plfstop) plfstrt = plfstop;
  275.  
  276.     /* ! If the masking operation is changed, the pfield_doline code could break
  277.      * on some systems (alignment) */
  278.     /* This actually seems to be correct now, at least the non-AGA stuff... */
  279.     plfstrt &= ~3;
  280.     plfstop &= ~3;
  281.     if ((fmode & 3) == 0)
  282.     plflinelen = (plfstop-plfstrt+15) & ~7;
  283.     else if ((fmode & 3) == 3)
  284.     plflinelen = (plfstop-plfstrt+63) & ~31;
  285.     else
  286.     plflinelen = (plfstop-plfstrt+31) & ~15;
  287.     
  288. }
  289.  
  290. /*
  291.  * Screen update macros/functions
  292.  */
  293.  
  294. static void decode_ham6 (int pix, int stoppos)
  295. {
  296.     static UWORD lastcolor;
  297.     ULONG *buf = ham_linebuf; 
  298.  
  299.     if (!bplham || bplplanecnt != 6)
  300.     return;
  301.     
  302.     if (pix <= diwfirstword) {
  303.     pix = diwfirstword;
  304.     lastcolor = bpl_info.color_regs[0];
  305.     }
  306.  
  307.     while (pix < diwlastword && pix < stoppos) {
  308.     int pv = pixdata.apixels[pix];
  309.     switch(pv & 0x30) {
  310.      case 0x00: lastcolor = bpl_info.color_regs[pv]; break;
  311.      case 0x10: lastcolor &= 0xFF0; lastcolor |= (pv & 0xF); break;
  312.      case 0x20: lastcolor &= 0x0FF; lastcolor |= (pv & 0xF) << 8; break;
  313.      case 0x30: lastcolor &= 0xF0F; lastcolor |= (pv & 0xF) << 4; break;
  314.     }
  315.  
  316.     buf[pix++] = lastcolor;
  317.     }    
  318. }
  319.  
  320. static void decode_ham_aga (int pix, int stoppos)
  321. {
  322.     static ULONG lastcolor;
  323.     ULONG *buf = ham_linebuf; 
  324.  
  325.     if (!bplham || (bplplanecnt != 6 && bplplanecnt != 8))
  326.     return;
  327.     
  328.     if (pix <= diwfirstword) {
  329.     pix = diwfirstword;
  330.     lastcolor = bpl_info.color_regs[0];
  331.     }
  332.  
  333.     if (bplplanecnt == 6) {
  334.     /* HAM 6 */
  335.     while (pix < diwlastword && pix < stoppos) {
  336.         int pv = pixdata.apixels[pix];
  337.         switch(pv & 0x30) {
  338.          case 0x00: lastcolor = bpl_info.color_regs[pv]; break;
  339.          case 0x10: lastcolor &= 0xFFFF00; lastcolor |= (pv & 0xF)*0x11; break;
  340.          case 0x20: lastcolor &= 0x00FFFF; lastcolor |= (pv & 0xF)*0x11 << 16; break;
  341.          case 0x30: lastcolor &= 0xFF00FF; lastcolor |= (pv & 0xF)*0x11 << 8; break;
  342.         }        
  343.         buf[pix++] = lastcolor;
  344.     }
  345.     } else if (bplplanecnt == 8) {
  346.     /* HAM 8 */
  347.     while (pix < diwlastword && pix < stoppos) {
  348.         int pv = pixdata.apixels[pix];
  349.         switch(pv & 0x3) {
  350.          case 0x0: lastcolor = bpl_info.color_regs[pv >> 2]; break;
  351.          case 0x1: lastcolor &= 0xFFFF03; lastcolor |= (pv & 0xFC); break;
  352.          case 0x2: lastcolor &= 0x03FFFF; lastcolor |= (pv & 0xFC) << 16; break;
  353.          case 0x3: lastcolor &= 0xFF03FF; lastcolor |= (pv & 0xFC) << 8; break;
  354.         }
  355.         buf[pix++] = lastcolor;
  356.     }
  357.     }
  358. }
  359.  
  360. #define LINE_TO_SCR(NAME, TYPE, DO_DOUBLE) \
  361. static void NAME(int pix, int stoppos, int offset) \
  362. { \
  363.     TYPE *buf = (TYPE *)xlinebuffer; \
  364.     int oldpix = pix; \
  365.     buf -= pix; \
  366.     if (DO_DOUBLE) offset /= sizeof(TYPE); \
  367.     while (pix < diwfirstword && pix < stoppos) { \
  368.         TYPE d = acolors[0]; \
  369.     buf[pix] = d; if (DO_DOUBLE) buf[pix+offset] = d; \
  370.     pix++; \
  371.     } \
  372.     if (bplham && bplplanecnt == 6) { \
  373.     /* HAM 6 */ \
  374.     while (pix < diwlastword && pix < stoppos) { \
  375.         TYPE d = xcolors[ham_linebuf[pix]]; \
  376.         buf[pix] = d; if (DO_DOUBLE) buf[pix+offset] = d; \
  377.         pix++; \
  378.     } \
  379.     } else if (bpldualpf) { \
  380.     /* Dual playfield */ \
  381.     int *lookup = bpldualpfpri ? dblpf_ind2 : dblpf_ind1; \
  382.     while (pix < diwlastword && pix < stoppos) { \
  383.         int pixcol = pixdata.apixels[pix]; \
  384.         TYPE d; \
  385.         if (spixstate[pix]) { \
  386.         d = acolors[pixcol]; \
  387.         } else { \
  388.         d = acolors[lookup[pixcol]]; \
  389.         } \
  390.         buf[pix] = d; if (DO_DOUBLE) buf[pix+offset] = d; \
  391.         pix++; \
  392.     } \
  393.     } else { \
  394.     while (pix < diwlastword && pix < stoppos) { \
  395.         TYPE d = acolors[pixdata.apixels[pix]]; \
  396.         buf[pix] = d; if (DO_DOUBLE) buf[pix+offset] = d; \
  397.         pix++; \
  398.     } \
  399.     } \
  400.     while (pix < stoppos) { \
  401.         TYPE d = acolors[0]; \
  402.     buf[pix] = d; if (DO_DOUBLE) buf[pix+offset] = d; \
  403.     pix++; \
  404.     } \
  405.     xlinebuffer = (char *)(((TYPE *)xlinebuffer) + pix - oldpix); \
  406. }
  407.  
  408. /* WARNING: Not too much of this will work correctly yet. */
  409.  
  410. static void pfield_linetoscr_aga(int pix, int stoppos)
  411. {
  412.     ULONG *buf = aga_lbufptr;
  413.     int i;
  414.     int xor = (UBYTE)(bpl_info.bplcon4 >> 8);
  415.     int oldpix = pix; \
  416.  
  417.     buf -= pix; \
  418.  
  419.     for (i = 0; i < stoppos; i++)
  420.     pixdata.apixels[i] ^= xor;
  421.     
  422.     while (pix < diwfirstword && pix < stoppos) {
  423.     buf[pix++] = bpl_info.color_regs[0];
  424.     }
  425.     if (bplham) {
  426.     while (pix < diwlastword && pix < stoppos) {
  427.         ULONG d = ham_linebuf[pix];
  428.         buf[pix] = d;
  429.         pix++;
  430.     }
  431.     } else if (bpldualpf) {
  432.     /* Dual playfield */
  433.     int *lookup = bpldualpfpri ? dblpf_aga2 : dblpf_aga1;
  434.     int *lookup_no = bpldualpfpri ? dblpf_2nd2 : dblpf_2nd1;
  435.     while (pix < diwlastword && pix < stoppos) {
  436.         int pixcol = pixdata.apixels[pix];
  437.         int pfno = lookup_no[pixcol];
  438.         
  439.         if (spixstate[pix]) {
  440.             buf[pix] = bpl_info.color_regs[pixcol];
  441.         } else {
  442.         int val = lookup[pixdata.apixels[pix]];
  443.         if (pfno == 2) 
  444.             val += dblpfofs[(bpl_info.bplcon2 >> 10) & 7];
  445.         buf[pix] = bpl_info.color_regs[val];
  446.         }
  447.         pix++;
  448.     }
  449.     } else if (bplehb) {
  450.     while (pix < diwlastword && pix < stoppos) {
  451.             int pixcol = pixdata.apixels[pix];
  452.         ULONG d = bpl_info.color_regs[pixcol];
  453.         /* What about sprites? */
  454.         if (pixcol & 0x20)
  455.         d = (d & 0x777777) >> 1;
  456.         buf[pix] = d;
  457.         pix++;
  458.     }
  459.     } else {
  460.     while (pix < diwlastword && pix < stoppos) {
  461.         int pixcol = pixdata.apixels[pix];
  462.         buf[pix] = bpl_info.color_regs[pixcol];
  463.         pix++;
  464.     }
  465.     }
  466.     while (pix < stoppos) {
  467.     buf[pix++] = bpl_info.color_regs[0];
  468.     }
  469.     aga_lbufptr += pix - oldpix;
  470. }
  471.  
  472. static void aga_translate32(int start, int stop)
  473. {
  474.     memcpy (((ULONG *)xlinebuffer) + start, aga_linebuf + start, 4*(stop-start));
  475. }
  476.  
  477. static void aga_translate16(int start, int stop)
  478. {
  479.     int i;
  480.     for (i = start; i < stop; i++) {
  481.     ULONG d = aga_linebuf[i];
  482.     UWORD v = ((d & 0xF0) >> 4) | ((d & 0xF000) >> 8) | ((d & 0xF00000) >> 12);
  483.     ((UWORD *)xlinebuffer)[i] = xcolors[v];
  484.     }
  485. }
  486.  
  487. static void aga_translate8(int start, int stop)
  488. {
  489.     int i;
  490.     for (i = start; i < stop; i++) {
  491.     ULONG d = aga_linebuf[i];
  492.     UWORD v = ((d & 0xF0) >> 4) | ((d & 0xF000) >> 8) | ((d & 0xF00000) >> 12);
  493.     ((UBYTE *)xlinebuffer)[i] = xcolors[v];
  494.     }
  495. }
  496.  
  497.  
  498.  
  499. #define FILL_LINE(NAME, TYPE) \
  500. static void NAME(char *buf) \
  501. { \
  502.     TYPE *b = (TYPE *)buf; \
  503.     int i;\
  504.     int maxpos = gfxvidinfo.maxlinetoscr; \
  505.     xcolnr col = acolors[0]; \
  506.     if (!maxpos) maxpos = 796; \
  507.     for (i = 0; i < maxpos; i++) \
  508.         *b++ = col; \
  509. }
  510.  
  511. LINE_TO_SCR(pfield_linetoscr_8, UBYTE, 0)
  512. LINE_TO_SCR(pfield_linetoscr_16, UWORD, 0)
  513. LINE_TO_SCR(pfield_linetoscr_32, ULONG, 0)
  514. LINE_TO_SCR(pfield_linetoscr_8_double_slow, UBYTE, 1)
  515. LINE_TO_SCR(pfield_linetoscr_16_double_slow, UWORD, 1)
  516. LINE_TO_SCR(pfield_linetoscr_32_double_slow, ULONG, 1)
  517.  
  518. FILL_LINE(fill_line_8, UBYTE)
  519. FILL_LINE(fill_line_16, UWORD)
  520. FILL_LINE(fill_line_32, ULONG)
  521.  
  522. #define pfield_linetoscr_full8 pfield_linetoscr_8
  523. #define pfield_linetoscr_full16 pfield_linetoscr_16
  524. #define pfield_linetoscr_full32 pfield_linetoscr_32
  525.  
  526. #define pfield_linetoscr_full8_double pfield_linetoscr_8_double_slow
  527. #define pfield_linetoscr_full16_double pfield_linetoscr_16_double_slow
  528. #define pfield_linetoscr_full32_double pfield_linetoscr_32_double_slow
  529.  
  530. #if 1 && defined(X86_ASSEMBLY)
  531. #undef pfield_linetoscr_full8
  532. #undef pfield_linetoscr_full16
  533. extern void pfield_linetoscr_full8(int, int, int) __asm__("pfield_linetoscr_full8");
  534. extern void pfield_linetoscr_full16(int, int, int) __asm__("pfield_linetoscr_full16");
  535. #undef pfield_linetoscr_full8_double
  536. #undef pfield_linetoscr_full16_double
  537.  
  538. static void pfield_linetoscr_full8_double(int start, int stop, int offset)
  539. {
  540.     char *oldxlb = (char *)xlinebuffer;
  541.     pfield_linetoscr_full8(start,stop,offset);
  542.     xlinebuffer = oldxlb + offset;
  543.     pfield_linetoscr_full8(start,stop,offset);
  544. }
  545. static void pfield_linetoscr_full16_double(int start, int stop, int offset)
  546. {
  547.     char *oldxlb = (char *)xlinebuffer;
  548.     pfield_linetoscr_full16(start,stop,offset);
  549.     xlinebuffer = oldxlb + offset;
  550.     pfield_linetoscr_full16(start,stop,offset);
  551. }
  552. #endif
  553.  
  554. static __inline__ void fill_line(int y)
  555. {
  556.     switch (gfxvidinfo.pixbytes) {
  557.      case 1: fill_line_8(gfxvidinfo.bufmem + gfxvidinfo.rowbytes * y); break;
  558.      case 2: fill_line_16(gfxvidinfo.bufmem + gfxvidinfo.rowbytes * y); break;
  559.      case 4: fill_line_32(gfxvidinfo.bufmem + gfxvidinfo.rowbytes * y); break;
  560.     }
  561. }
  562.  
  563. static void pfield_do_linetoscr(int start, int stop)
  564. {
  565.     int factor = use_lores ? 1 : 2;
  566.     int oldstop = stop;
  567.     int real_start, real_stop;
  568.     
  569.     start = PIXEL_XPOS(start);
  570.     if (start < 8*factor)
  571.     start = 8*factor;
  572.     stop = PIXEL_XPOS(stop);
  573.     if (stop > 406*factor)
  574.     stop = 406*factor;
  575.     
  576.     if (start >= stop)
  577.     return;
  578.     if (stop <= gfxvidinfo.x_adjust)
  579.     return;
  580.     
  581.     slowline_lasttoscr = oldstop;
  582.  
  583.     if (start < gfxvidinfo.x_adjust)
  584.     real_start = gfxvidinfo.x_adjust;
  585.     else
  586.     real_start = start;
  587.     
  588.     if (gfxvidinfo.maxlinetoscr) {
  589.     real_stop = gfxvidinfo.x_adjust + gfxvidinfo.maxlinetoscr;
  590.     if (real_stop > stop)
  591.         real_stop = stop;
  592.     } else
  593.     real_stop = stop;
  594.  
  595. #if AGA_CHIPSET == 0
  596.     if (start == 8*factor && stop == 406*factor) {
  597.     switch (gfxvidinfo.pixbytes) {
  598.      case 1: pfield_linetoscr_full8 (real_start, real_stop, 0); break;
  599.      case 2: pfield_linetoscr_full16 (real_start, real_stop, 0); break;
  600.      case 4: pfield_linetoscr_full32 (real_start, real_stop, 0); break;
  601.     }
  602.     } else {
  603.     if (real_start >= real_stop)
  604.         return;
  605.     switch (gfxvidinfo.pixbytes) {
  606.      case 1: pfield_linetoscr_8 (real_start, real_stop, 0); break;
  607.      case 2: pfield_linetoscr_16 (real_start, real_stop, 0); break;
  608.      case 4: pfield_linetoscr_32 (real_start, real_stop, 0); break;
  609.     }
  610.     }
  611. #else
  612.     pfield_linetoscr_aga(real_start, real_stop);
  613. #endif
  614. }
  615.  
  616. static void pfield_do_linetoscr_full(int double_line)
  617. {
  618.     int factor = use_lores ? 1 : 2;
  619.     int stop, start;
  620.     
  621.     start = 8*factor;
  622.     stop = 406*factor;
  623.     
  624.     if (start < gfxvidinfo.x_adjust)
  625.     start = gfxvidinfo.x_adjust;
  626.     
  627.     if (gfxvidinfo.maxlinetoscr) {
  628.     int tmp = gfxvidinfo.x_adjust + gfxvidinfo.maxlinetoscr;
  629.     if (tmp < stop)
  630.         stop = tmp;
  631.     }
  632.  
  633. #if AGA_CHIPSET == 0
  634.     if (double_line) {
  635.     switch (gfxvidinfo.pixbytes) {
  636.      case 1: pfield_linetoscr_full8_double (start, stop, gfxvidinfo.rowbytes); break;
  637.      case 2: pfield_linetoscr_full16_double (start, stop, gfxvidinfo.rowbytes); break;
  638.      case 4: pfield_linetoscr_full32_double (start, stop, gfxvidinfo.rowbytes); break;
  639.     }
  640.     } else 
  641.     switch (gfxvidinfo.pixbytes) {
  642.      case 1: pfield_linetoscr_full8 (start, stop, 0); break;
  643.      case 2: pfield_linetoscr_full16 (start, stop, 0); break;
  644.      case 4: pfield_linetoscr_full32 (start, stop, 0); break;
  645.     }
  646. #else
  647.     pfield_linetoscr_aga(start, stop);
  648. #endif
  649. }
  650.  
  651. /*
  652.  * This function is called whenever a hardware register that controls the
  653.  * screen display is modified. Usually, this routine does nothing. But in
  654.  * some cases, e.g., when a color changes in mid-screen, as in copper-plasma
  655.  * effects, this function switches the update method from the fast full-line
  656.  * update to the much slower single-color-clock update.
  657.  */
  658. static void pfield_may_need_update(int colreg)
  659. {
  660.     int i;
  661.     
  662.     /* Ignore, if this happened before or after the DDF window */
  663.     /* @@@ This breaks some copper plasmas. Not good... maybe a config
  664.      * option is needed. */
  665.     if (framecnt != 0 || !pfield_linedmaon || current_hpos() <= plfstrt
  666.     || vpos < plffirstline || vpos < minfirstline || vpos >= plflastline
  667.     || next_lineno >= gfxvidinfo.maxline)
  668.     {
  669.         return;
  670.     }
  671.     /* 
  672.      * If a color reg was modified, it is only important if we are within
  673.      * the DIW.
  674.      */
  675.     if (PIXEL_XPOS(current_hpos()) <= diwfirstword && colreg)
  676.         return;
  677.  
  678.     /*
  679.      * If we are past the DDF window, me might as well draw the complete
  680.      * line now.
  681.      */
  682.     if (current_hpos() > plfstrt + plflinelen && pfield_fullline) {
  683.     if (!pfield_linedone)
  684.         pfield_doline();
  685.     pfield_linedone = 1;
  686.     return;
  687.     }
  688.         
  689.     do_sprites(vpos, current_hpos());
  690.     if (pfield_fullline) {
  691.     pfield_lastpart_hpos = 0;
  692.     memset(pixdata.apixels, 0, sizeof(pixdata.apixels));
  693.     memset(spixstate,0,sizeof spixstate);
  694.     pfield_fullline = 0;
  695.     slowline_nextpos = -1;
  696.     slowline_linepos = 0;
  697.     slowline_lasttoscr = 0;
  698.     } else {    
  699.     assert(pfield_lastpart_hpos <= current_hpos());
  700.     }
  701.     for (i = pfield_lastpart_hpos; i < current_hpos(); i++) {
  702.     pfield_doline_slow(i);
  703.     }
  704.     if (colreg) {
  705.     pfield_do_linetoscr(slowline_lasttoscr, current_hpos());
  706.     }
  707.     pfield_lastpart_hpos = current_hpos();
  708. }
  709.  
  710. /* Apparently, the DMA bit is tested by the hardware at some point,
  711.  * presumably at the ddfstart position, to determine whether it
  712.  * ought to draw the line.
  713.  * This is probably not completely correct, but should not matter
  714.  * very much.
  715.  */
  716. static void pfield_calclinedma(void)
  717. {
  718.     if (current_hpos() >= plfstrt)
  719.         return;
  720.     
  721.     pfield_linedmaon = dmaen(DMA_BITPLANE);
  722. }
  723.  
  724.  /* 
  725.   * register functions
  726.   */
  727.  
  728. static UWORD DMACONR(void)
  729. {
  730.     return (dmacon | (bltstate==BLT_done ? 0 : 0x4000)
  731.         | (blt_info.blitzero ? 0x2000 : 0));
  732. }
  733. static UWORD INTENAR(void) { return intena; }
  734. UWORD INTREQR(void)
  735. {
  736.     return intreq | (use_serial ? 0x0001 : 0);
  737. }
  738. static UWORD ADKCONR(void) { return adkcon; }
  739. static UWORD VPOSR(void) 
  740. {
  741. #if AGA_CHIPSET == 1
  742.     return (vpos >> 8) | lof | 0x2300;
  743. #elif defined (ECS_AGNUS)
  744.     return (vpos >> 8) | lof | 0x2000;
  745. #else
  746.     return (vpos >> 8) | lof;
  747. #endif
  748. }
  749. static void  VPOSW(UWORD v)  { lof = v & 0x8000; }
  750. static UWORD VHPOSR(void) { return (vpos << 8) | current_hpos(); } 
  751.  
  752. static void  COP1LCH(UWORD v) { cop1lc= (cop1lc & 0xffff) | ((ULONG)v << 16); }
  753. static void  COP1LCL(UWORD v) { cop1lc= (cop1lc & ~0xffff) | v; }
  754. static void  COP2LCH(UWORD v) { cop2lc= (cop2lc & 0xffff) | ((ULONG)v << 16); }
  755. static void  COP2LCL(UWORD v) { cop2lc= (cop2lc & ~0xffff) | v; }
  756.  
  757. static void  COPJMP1(UWORD a)
  758. {
  759.     coplc = cop1lc; copstate = COP_read; 
  760.     eventtab[ev_copper].active = 1; eventtab[ev_copper].oldcycles = cycles;
  761.     eventtab[ev_copper].evtime = 4 + cycles; events_schedule();
  762.     copper_active = 1;
  763. }
  764. static void  COPJMP2(UWORD a)
  765. {
  766.     coplc = cop2lc; copstate = COP_read; 
  767.     eventtab[ev_copper].active = 1; eventtab[ev_copper].oldcycles = cycles;
  768.     eventtab[ev_copper].evtime = 4 + cycles; events_schedule();
  769.     copper_active = 1;
  770. }
  771.  
  772. static void  DMACON(UWORD v) 
  773. {
  774.     int i, need_resched = 0;
  775.  
  776.     UWORD oldcon = dmacon;
  777.     setclr(&dmacon,v); dmacon &= 0x1FFF;
  778.     pfield_calclinedma();
  779.     
  780.     /* FIXME? Maybe we need to think a bit more about the master DMA enable 
  781.      * bit in these cases. */
  782.     if ((dmacon & DMA_COPPER) > (oldcon & DMA_COPPER)) { 
  783.     COPJMP1(0);
  784.     }
  785.     if ((dmacon & DMA_SPRITE) > (oldcon & DMA_SPRITE)) {
  786.     int i;
  787.     for (i = 0; i < 8; i++)
  788.         spron[i] = 1;
  789.     }
  790.     if ((dmacon & DMA_BLITPRI) > (oldcon & DMA_BLITPRI) && bltstate != BLT_done) {
  791.     static int count = 0;
  792.     if (!count) {
  793.         count = 1;
  794.         fprintf(stderr, "warning: program is doing blitpri hacks.\n");
  795.     }
  796.     regs.spcflags |= SPCFLAG_BLTNASTY;
  797.     }
  798. #ifndef DONT_WANT_SOUND
  799.     for (i = 0; i < 4; i++) {
  800.     struct audio_channel_data *cdp = audio_channel + i;
  801.     
  802.     cdp->dmaen = (dmacon & 0x200) && (dmacon & (1<<i));
  803.     if (cdp->dmaen) {
  804.         if (cdp->state == 0) {
  805.         cdp->state = 1;
  806.         cdp->pt = cdp->lc;
  807.         cdp->wper = cdp->per;
  808.         cdp->wlen = cdp->len;
  809.         cdp->data_written = 2;
  810.         eventtab[ev_aud0 + i].oldcycles = eventtab[ev_hsync].oldcycles;
  811.         eventtab[ev_aud0 + i].evtime = eventtab[ev_hsync].evtime;
  812.         eventtab[ev_aud0 + i].active = 1;
  813.         need_resched = 1; /* not _really_ necessary here, but... */
  814.         }
  815.     } else {
  816.         if (cdp->state == 1 || cdp->state == 5) {
  817.         cdp->state = 0;
  818.         cdp->current_sample = 0;
  819.         eventtab[ev_aud0 + i].active = 0;
  820.         need_resched = 1;
  821.         }
  822.     }
  823.     }
  824. #endif
  825.     if (copper_active && !eventtab[ev_copper].active) {
  826.     eventtab[ev_copper].active = 1;
  827.     eventtab[ev_copper].oldcycles = cycles;
  828.     eventtab[ev_copper].evtime = 1 + cycles;
  829.     need_resched = 1;
  830.     }
  831.     if (need_resched)
  832.     events_schedule();
  833. }
  834. static void  INTENA(UWORD v) { setclr(&intena,v); regs.spcflags |= SPCFLAG_INT; }
  835. void  INTREQ(UWORD v)
  836. {
  837.   setclr(&intreq,v);
  838.   regs.spcflags |= SPCFLAG_INT;
  839.   if ((v&0x8800)==0x0800) serdat&=0xbfff;
  840.  }
  841.  
  842. static void  ADKCON(UWORD v) { setclr(&adkcon,v); }
  843.  
  844. static void  BPLPTH(UWORD v, int num) { bplpt[num] = (bplpt[num] & 0xffff) | ((ULONG)v << 16); }
  845. static void  BPLPTL(UWORD v, int num) { bplpt[num] = (bplpt[num] & ~0xffff) | (v & 0xFFFE); }
  846.  
  847. /*
  848.  * I've seen the listing of an example program that changes 
  849.  * from lo- to hires while a line is being drawn. That's
  850.  * awful, but we want to emulate it.
  851.  */
  852. static void  BPLCON0(UWORD v) 
  853. {
  854.     if (bpl_info.bplcon0 == v)
  855.     return;
  856.     pfield_may_need_update(0);
  857.     bpl_info.bplcon0 = v;
  858.     bplhires = (v & 0x8000) == 0x8000;
  859.     bplplanecnt = (v & 0x7000) >> 12;
  860.     bplham = (v & 0x800) == 0x800;
  861.     bpldualpf = (v & 0x400) == 0x400;
  862.     bplehb = (v & 0xFDC0) == 0x6000 && !(bpl_info.bplcon2 & 0x200); /* see below */
  863.     calcdiw(); /* This should go away. */
  864. }
  865. static void  BPLCON1(UWORD v) 
  866. {
  867.     if (bpl_info.bplcon1 == v)
  868.     return;
  869.     pfield_may_need_update(0);
  870.     bpl_info.bplcon1 = v; 
  871.     bpldelay1 = v & 0xF; 
  872.     bpldelay2 = (v >> 4) & 0xF; 
  873. }
  874. static void  BPLCON2(UWORD v) 
  875. {
  876.     if (bpl_info.bplcon2 == v)
  877.     return;
  878.     pfield_may_need_update(0); 
  879.     bpl_info.bplcon2 = v;
  880.     bpldualpfpri = (v & 0x40) == 0x40;
  881.     plfpri[1] = 1 << 2*(v & 7);
  882.     plfpri[2] = 1 << 2*((v>>3) & 7);
  883.     bplehb = (bpl_info.bplcon0 & 0xFDC0) == 0x6000 && !(v & 0x200); /* see above */
  884. }
  885. static void  BPLCON3(UWORD v) 
  886. {
  887.     if (bpl_info.bplcon3 == v)
  888.     return;
  889.     pfield_may_need_update(0); 
  890.     bpl_info.bplcon3 = v; 
  891. }
  892. static void  BPLCON4(UWORD v) 
  893. {
  894.     if (bpl_info.bplcon4 == v)
  895.     return;
  896.     pfield_may_need_update(0); 
  897.     bpl_info.bplcon4 = v; 
  898. }
  899.  
  900. static void  BPL1MOD(UWORD v)
  901. {
  902.     v &= ~1;
  903.     if (bpl1mod == v)
  904.     return;
  905.     pfield_may_need_update(0); 
  906.     bpl1mod = v;
  907. }
  908. static void  BPL2MOD(UWORD v)
  909.     v &= ~1;
  910.     if (bpl2mod == v)
  911.     return;
  912.     pfield_may_need_update(0); 
  913.     bpl2mod = v;
  914. }
  915.  
  916. static void  BPL1DAT(UWORD v) { bpl1dat = v; }
  917. static void  BPL2DAT(UWORD v) { bpl2dat = v; }
  918. static void  BPL3DAT(UWORD v) { bpl3dat = v; }
  919. static void  BPL4DAT(UWORD v) { bpl4dat = v; }
  920. static void  BPL5DAT(UWORD v) { bpl5dat = v; }
  921. static void  BPL6DAT(UWORD v) { bpl6dat = v; }
  922.  
  923. /* We call pfield_may_need_update() from here. Actually, 
  924.  * I have no idea what happens if someone changes ddf or
  925.  * diw mid-line, and I don't really want to know. I doubt
  926.  * that this sort of thing was ever used to create a
  927.  * useful effect.
  928.  */
  929. static void  DIWSTRT(UWORD v) 
  930. {
  931.     if (bpl_info.diwstrt == v)
  932.     return;
  933.     pfield_may_need_update(0);
  934.     bpl_info.diwstrt = v; 
  935.     calcdiw();
  936. }
  937. static void  DIWSTOP(UWORD v)
  938. {
  939.     if (bpl_info.diwstop == v)
  940.     return;
  941.     pfield_may_need_update(0);
  942.     bpl_info.diwstop = v; 
  943.     calcdiw();
  944. }
  945. static void  DDFSTRT(UWORD v)
  946.     if (bpl_info.ddfstrt == v)
  947.     return;
  948.     pfield_may_need_update(0);
  949.     bpl_info.ddfstrt = v; 
  950.     calcdiw();
  951. }
  952. static void  DDFSTOP(UWORD v) 
  953.     if (bpl_info.ddfstop == v)
  954.     return;
  955.     pfield_may_need_update(0); 
  956.     bpl_info.ddfstop = v;
  957.     calcdiw();
  958. }
  959.  
  960. static void  BLTADAT(UWORD v) 
  961. {
  962.     maybe_blit(); 
  963.     blt_info.bltadat = v; 
  964. }
  965. static void  BLTBDAT(UWORD v)
  966. {
  967.     maybe_blit(); 
  968.     blt_info.bltbdat = v; 
  969. }
  970. static void  BLTCDAT(UWORD v) { maybe_blit(); blt_info.bltcdat = v; }
  971.  
  972. static void  BLTAMOD(UWORD v) { maybe_blit(); blt_info.bltamod = v & 0xFFFE; }
  973. static void  BLTBMOD(UWORD v) { maybe_blit(); blt_info.bltbmod = v & 0xFFFE; }
  974. static void  BLTCMOD(UWORD v) { maybe_blit(); blt_info.bltcmod = v & 0xFFFE; }
  975. static void  BLTDMOD(UWORD v) { maybe_blit(); blt_info.bltdmod = v & 0xFFFE; }
  976.  
  977. static void  BLTCON0(UWORD v) { maybe_blit(); bltcon0 = v; blinea_shift = v >> 12; }
  978. /* The next category is "Most useless hardware register". 
  979.  * And the winner is... */
  980. static void  BLTCON0L(UWORD v) { maybe_blit(); bltcon0 = (bltcon0 & 0xFF00) | (v & 0xFF); }
  981. static void  BLTCON1(UWORD v) { maybe_blit(); bltcon1 = v; }
  982.  
  983. static void  BLTAFWM(UWORD v) { maybe_blit(); blt_info.bltafwm = v; }
  984. static void  BLTALWM(UWORD v) { maybe_blit(); blt_info.bltalwm = v; }
  985.  
  986. static void  BLTAPTH(UWORD v) { maybe_blit(); bltapt= (bltapt & 0xffff) | ((ULONG)(v & 0x1F) << 16); }
  987. static void  BLTAPTL(UWORD v) { maybe_blit(); bltapt= (bltapt & ~0xffff) | (v & 0xFFFE); }
  988. static void  BLTBPTH(UWORD v) { maybe_blit(); bltbpt= (bltbpt & 0xffff) | ((ULONG)(v & 0x1F) << 16); }
  989. static void  BLTBPTL(UWORD v) { maybe_blit(); bltbpt= (bltbpt & ~0xffff) | (v & 0xFFFE); }
  990. static void  BLTCPTH(UWORD v) { maybe_blit(); bltcpt= (bltcpt & 0xffff) | ((ULONG)(v & 0x1F) << 16); }
  991. static void  BLTCPTL(UWORD v) { maybe_blit(); bltcpt= (bltcpt & ~0xffff) | (v & 0xFFFE); }
  992. static void  BLTDPTH(UWORD v) { maybe_blit(); bltdpt= (bltdpt & 0xffff) | ((ULONG)(v & 0x1F) << 16); }
  993. static void  BLTDPTL(UWORD v) { maybe_blit(); bltdpt= (bltdpt & ~0xffff) | (v & 0xFFFE); }
  994. static void  BLTSIZE(UWORD v) 
  995. {
  996.     bltsize = v;
  997.     
  998.     maybe_blit(); 
  999.  
  1000.     blt_info.vblitsize = bltsize >> 6;
  1001.     blt_info.hblitsize = bltsize & 0x3F;
  1002.     if (!blt_info.vblitsize) blt_info.vblitsize = 1024;
  1003.     if (!blt_info.hblitsize) blt_info.hblitsize = 64;
  1004.     
  1005.     bltstate = BLT_init; 
  1006.     regs.spcflags |= SPCFLAG_BLIT; 
  1007. }
  1008. static void BLTSIZV(UWORD v) 
  1009. {
  1010.     maybe_blit(); 
  1011.     oldvblts = v & 0x7FFF;
  1012. }
  1013. static void BLTSIZH(UWORD v) 
  1014. {
  1015.     maybe_blit(); 
  1016.     blt_info.hblitsize = v & 0x7FF;
  1017.     blt_info.vblitsize = oldvblts;
  1018.     if (!blt_info.vblitsize) blt_info.vblitsize = 32768;
  1019.     if (!blt_info.hblitsize) blt_info.hblitsize = 0x800;
  1020.     bltstate = BLT_init; 
  1021.     regs.spcflags |= SPCFLAG_BLIT;
  1022. }
  1023. static void  SPRxCTL_1(UWORD v, int num)
  1024. {
  1025.     bpl_info.sprctl[num] = v;
  1026.     bpl_info.sprarmed[num] = 0;
  1027.     if (bpl_info.sprpos[num] == 0 && v == 0)
  1028.     spron[num] = 0;
  1029.     else
  1030.     spron[num] |= 2;
  1031. }
  1032. static void  SPRxPOS_1(UWORD v, int num) 
  1033. {
  1034.     bpl_info.sprpos[num] = v; 
  1035. }
  1036. static void  SPRxDATA_1(UWORD v, int num)
  1037. {
  1038.     bpl_info.sprdata[num] = v;
  1039.     bpl_info.sprarmed[num] = 1; 
  1040. }
  1041. static void  SPRxDATB_1(UWORD v, int num) 
  1042. {
  1043.     bpl_info.sprdatb[num] = v;
  1044. }
  1045. static void  SPRxCTL(UWORD v, int num) { pfield_may_need_update(0); SPRxCTL_1(v, num); }
  1046. static void  SPRxPOS(UWORD v, int num) { pfield_may_need_update(0); SPRxPOS_1(v, num); }
  1047. static void  SPRxDATA(UWORD v, int num){ pfield_may_need_update(0); SPRxDATA_1(v, num); }
  1048. static void  SPRxDATB(UWORD v, int num){ pfield_may_need_update(0); SPRxDATB_1(v, num); }
  1049. static void  SPRxPTH(UWORD v, int num)
  1050. {
  1051.     sprpt[num] &= 0xffff; 
  1052.     sprpt[num] |= (ULONG)v << 16; 
  1053.     if (!spron[num]) spron[num] = 1; 
  1054. }
  1055. static void  SPRxPTL(UWORD v, int num)
  1056. {
  1057.     sprpt[num] &= ~0xffff; 
  1058.     sprpt[num] |= v; 
  1059.     if (!spron[num]) spron[num] = 1; 
  1060. }
  1061.  
  1062. static void  COLOR(UWORD v, int num)
  1063. {
  1064.     int r,g,b;
  1065.     int cr,cg,cb;
  1066.     int colreg;
  1067.     
  1068.     v &= 0xFFF;
  1069. #if AGA_CHIPSET == 1
  1070.     {
  1071.     ULONG cval;
  1072.     colreg = ((bpl_info.bplcon3 >> 13) & 7) * 32 + num;
  1073.     r = (v & 0xF00) >> 8;
  1074.     g = (v & 0xF0) >> 4;
  1075.     b = (v & 0xF) >> 0;
  1076.     cr = bpl_info.color_regs[colreg] >> 16;
  1077.     cg = (bpl_info.color_regs[colreg] >> 8) & 0xFF;
  1078.     cb = bpl_info.color_regs[colreg] & 0xFF;
  1079.  
  1080.     if (bpl_info.bplcon3 & 0x200) {
  1081.         cr &= 0xF0; cr |= r;
  1082.         cg &= 0xF0; cg |= g;
  1083.         cb &= 0xF0; cb |= b;
  1084.     } else {
  1085.         cr = r + (r << 4);
  1086.         cg = g + (g << 4);
  1087.         cb = b + (b << 4);
  1088.     }
  1089.     cval = (cr << 16) | (cg << 8) | cb;
  1090.     if (cval == bpl_info.color_regs[colreg])
  1091.         return;
  1092.     bpl_info.color_regs[colreg] = cval;
  1093.     pfield_may_need_update(1);
  1094.     }
  1095. #else 
  1096.     {
  1097.     if (bpl_info.color_regs[num] == v)
  1098.         return;
  1099.     pfield_may_need_update(1);
  1100.     bpl_info.color_regs[num] = v;
  1101.     acolors[num] = xcolors[v];
  1102.     acolors[num+32] = xcolors[(v >> 1) & 0x777];
  1103.     }
  1104. #endif
  1105. }
  1106.  
  1107. static void  DSKSYNC(UWORD v) { dsksync = v; }
  1108. static void  DSKDAT(UWORD v) { fprintf(stderr, "DSKDAT written. Not good.\n"); }
  1109. static void  DSKPTH(UWORD v) { dskpt = (dskpt & 0xffff) | ((ULONG)v << 16); }
  1110. static void  DSKPTL(UWORD v) { dskpt = (dskpt & ~0xffff) | (v); }
  1111.  
  1112. static void  DSKLEN(UWORD v) 
  1113. {
  1114.     if (v & 0x8000) {
  1115.     dskdmaen = dskdmaen == 1 ? 2 : 1;
  1116.     } else { 
  1117.     dskdmaen = 0;
  1118.     if (eventtab[ev_diskblk].active)
  1119.         fprintf(stderr, "warning: Disk DMA aborted!\n");
  1120.     eventtab[ev_diskblk].active = 0;
  1121.     events_schedule();
  1122.     
  1123.     }
  1124.     dsklen = dsklength = v; dsklength &= 0x3fff;
  1125.     if (dskdmaen == 2 && dsksync != 0x4489 && (adkcon & 0x400)) {
  1126.     fprintf(stderr, "Non-standard sync: %04x len: %x\n", dsksync, dsklength);
  1127.     }
  1128.     if (dskdmaen > 1) {
  1129.     if (dsklen & 0x4000) {
  1130.         eventtab[ev_diskblk].active = 1;
  1131.         eventtab[ev_diskblk].oldcycles = cycles;
  1132.         eventtab[ev_diskblk].evtime = 40 + cycles; /* ??? */
  1133.         events_schedule();
  1134.     } else {
  1135.         int result = DISK_PrepareReadMFM(dsklength, dsksync, adkcon & 0x400);
  1136.         if (result) {
  1137.         eventtab[ev_diskblk].active = 1;
  1138.         eventtab[ev_diskblk].oldcycles = cycles;
  1139.         eventtab[ev_diskblk].evtime = result + cycles;
  1140.         events_schedule();
  1141.         }
  1142.     }
  1143.     }
  1144. }
  1145.  
  1146. static UWORD DSKBYTR(void)
  1147. {
  1148.     UWORD v = (dsklen >> 1) & 0x6000;
  1149.     UWORD mfm, byte;
  1150.     if (DISK_GetData(&mfm, &byte))
  1151.     v |= 0x8000;
  1152.     v |= byte;
  1153.     if (dsksync == mfm) v |= 0x1000;
  1154.     return v;
  1155. }
  1156.  
  1157. static UWORD DSKDATR(void) 
  1158. {
  1159.     UWORD mfm, byte;
  1160.     DISK_GetData(&mfm, &byte);
  1161.     return mfm; 
  1162. }
  1163. static UWORD POTGOR(void)
  1164. {
  1165.     UWORD v = 0xFF00;
  1166.     if (buttonstate[2])
  1167.     v &= 0xFBFF;
  1168.  
  1169.     if (buttonstate[1])
  1170.     v &= 0xFEFF;
  1171.     
  1172.     return v;
  1173. }
  1174. static UWORD POT0DAT(void)
  1175. {
  1176.     static UWORD cnt = 0;
  1177.     if (buttonstate[2])
  1178.     cnt = ((cnt + 1) & 0xFF) | (cnt & 0xFF00);
  1179.     if (buttonstate[1])
  1180.     cnt += 0x100;
  1181.     
  1182.     return cnt;
  1183. }
  1184. static UWORD JOY0DAT(void) { return joy0x + (joy0y << 8); }
  1185. static UWORD JOY1DAT(void)
  1186. {
  1187.     return joy0dir;
  1188. }
  1189. static void JOYTEST(UWORD v)
  1190. {
  1191.     joy0x = joy1x = v & 0xFC;
  1192.     joy0y = joy1y = (v >> 8) & 0xFC;    
  1193. }
  1194. static void AUDxLCH(int nr, UWORD v) { audio_channel[nr].lc = (audio_channel[nr].lc & 0xffff) | ((ULONG)v << 16); }
  1195. static void AUDxLCL(int nr, UWORD v) { audio_channel[nr].lc = (audio_channel[nr].lc & ~0xffff) | (v & 0xFFFE); }
  1196. static void AUDxPER(int nr, UWORD v)
  1197. {
  1198.     static int warned = 0;
  1199.     if (v <= 0) {
  1200.     if (!warned)
  1201.         fprintf(stderr, "Broken program accessing the sound hardware\n"), warned++;
  1202.     v = 65535;
  1203.     }
  1204.  
  1205.     if (v < maxhpos/2 && produce_sound < 3)
  1206.     v = maxhpos/2;
  1207.  
  1208.     audio_channel[nr].per = v;
  1209. }
  1210.  
  1211. static void AUDxVOL(int nr, UWORD v) { audio_channel[nr].vol = v & 64 ? 63 : v & 63; }
  1212. static void AUDxLEN(int nr, UWORD v) { audio_channel[nr].len = v; }
  1213.  
  1214. static int copcomp(void)
  1215. {
  1216.     UWORD vp = vpos & (((copi2 >> 8) & 0x7F) | 0x80);
  1217.     UWORD hp = current_hpos() & (copi2 & 0xFE);
  1218.     UWORD vcmp = copi1 >> 8;
  1219.     UWORD hcmp = copi1 & 0xFE;
  1220.     return (vp > vcmp || (vp == vcmp && hp >= hcmp)) && ((copi2 & 0x8000) || !(DMACONR() & 0x4000));
  1221. }
  1222.  
  1223. /*
  1224.  * Calculate the minimum number of cycles after which the
  1225.  * copper comparison becomes true. This is quite tricky. I hope it works.
  1226.  */
  1227. static int calc_copcomp_true(int currvpos, int currhpos)
  1228. {    
  1229.     UWORD vp = currvpos & (((copi2 >> 8) & 0x7F) | 0x80);
  1230.     UWORD hp = currhpos & (copi2 & 0xFE);
  1231.     UWORD vcmp = copi1 >> 8;
  1232.     UWORD hcmp = copi1 & 0xFE;
  1233.     int copper_time_hpos;
  1234.     int cycleadd = maxhpos - currhpos;
  1235.     int coptime = 0;
  1236.  
  1237.     if ((vp > vcmp || (vp == vcmp && hp >= hcmp)) && ((copi2 & 0x8000) || !(DMACONR() & 0x4000)))
  1238.         return 0;    
  1239.  
  1240.     try_again:
  1241.  
  1242.     while (vp < vcmp) {
  1243.     currvpos++;
  1244.     if (currvpos > maxvpos + 1)
  1245.         return -1;
  1246.     currhpos = 0;
  1247.     coptime += cycleadd;
  1248.     cycleadd = maxhpos;
  1249.     vp = currvpos & (((copi2 >> 8) & 0x7F) | 0x80);
  1250.     }
  1251.     if (coptime > 0 && bplhires && bplplanecnt == 4)
  1252.     return coptime;
  1253.     copper_time_hpos = currhpos;
  1254.     hp = copper_time_hpos & (copi2 & 0xFE);
  1255.     if (!(vp > vcmp)) {    
  1256.     while (hp < hcmp-2) {
  1257.         currhpos++;
  1258.         /* Copper DMA is turned off in Hires 4 bitplane mode */
  1259.         if (!bplhires || bplplanecnt < 4 || !dmaen(DMA_BITPLANE)
  1260.         || currhpos < plfstrt-2 || currhpos > (plfstop+4))
  1261.         copper_time_hpos++;
  1262.         
  1263.         if (currhpos > maxhpos-4) {
  1264.         /* Now, what? There might be a good position on the
  1265.          * next line. But it can also be the FFFF FFFE
  1266.          * case.
  1267.          */
  1268.         currhpos = 0;
  1269.         currvpos++;
  1270.         vp = currvpos & (((copi2 >> 8) & 0x7F) | 0x80);
  1271.         goto try_again;
  1272.         }
  1273.         coptime++;
  1274.         hp = copper_time_hpos & (copi2 & 0xFE);
  1275.     }
  1276.     }
  1277.     if (coptime == 0) /* waiting for the blitter */
  1278.         return 1;
  1279.  
  1280.     return coptime;
  1281. }
  1282.  
  1283. static void copper_read(void)
  1284. {
  1285.     if (dmaen(DMA_COPPER)){
  1286.     copi1 = chipmem_bank.wget(coplc); 
  1287.     copi2 = chipmem_bank.wget(coplc+2);
  1288.     coplc += 4;
  1289.     eventtab[ev_copper].oldcycles = cycles;
  1290.     eventtab[ev_copper].evtime = ((copi1 & 1) ? (copi2 & 1) ? 10 : 8 : 4) + cycles;
  1291.     copstate = (copi1 & 1) ? (copi2 & 1) ? COP_skip : COP_wait : COP_move;
  1292.     } else {
  1293.     copstate = COP_read;
  1294.     eventtab[ev_copper].active = 0;
  1295.     }
  1296. }
  1297.  
  1298. static void do_copper(void)
  1299. {    
  1300.     switch(copstate){
  1301.      case COP_read:
  1302.     copper_read();
  1303.     break;
  1304.      case COP_move:
  1305.     if (copi1 >= (copcon & 2 ? 0x40 : 0x80)) {
  1306.         custom_bank.wput(copi1,copi2);
  1307.         copper_read();
  1308.     } else {
  1309.         copstate = COP_stop;
  1310.         eventtab[ev_copper].active = 0;
  1311.         copper_active = 0;
  1312.     }
  1313.     break;
  1314.      case COP_skip:
  1315.     if (calc_copcomp_true(vpos, current_hpos()) == 0)
  1316.         coplc += 4;
  1317.     copper_read();
  1318.     break;
  1319.      case COP_wait: {        
  1320.      int coptime = calc_copcomp_true(vpos, current_hpos());
  1321.      if (coptime < 0) {
  1322.          copstate = COP_stop;
  1323.          eventtab[ev_copper].active = 0;
  1324.          copper_active = 0;
  1325.      } else {         
  1326.          if (!coptime)
  1327.               copper_read();
  1328.          else {
  1329.          eventtab[ev_copper].evtime = coptime + cycles;
  1330.          eventtab[ev_copper].oldcycles = cycles;
  1331.          }
  1332.      }
  1333.      break;
  1334.      }
  1335.      case COP_stop:
  1336.     eventtab[ev_copper].active = 0;
  1337.     copper_active = 0;
  1338.     break;
  1339.     }
  1340. }
  1341.  
  1342. static void diskblk_handler(void)
  1343. {
  1344.     regs.spcflags |= SPCFLAG_DISK;
  1345.     eventtab[ev_diskblk].active = 0;
  1346. }
  1347.  
  1348. void do_disk(void)
  1349. {
  1350.     if (dskdmaen != 2 && (regs.spcflags & SPCFLAG_DISK)) {
  1351.     fprintf(stderr, "BUG!\n");
  1352.     return;
  1353.     }
  1354.     if (dmaen(0x10)){
  1355.     if (dsklen & 0x4000) {
  1356.         if (!chipmem_bank.check (dskpt, 2*dsklength)) {
  1357.         fprintf(stderr, "warning: Bad disk write DMA pointer\n");
  1358.         } else {
  1359.         UBYTE *mfmp = get_real_address (dskpt);
  1360.         int i;
  1361.         DISK_InitWrite();
  1362.  
  1363.         for (i = 0; i < dsklength; i++) {
  1364.             UWORD d = (*mfmp << 8) + *(mfmp+1);
  1365.             mfmwrite[i] = d;
  1366.             mfmp += 2;
  1367.         }
  1368.         DISK_WriteData(dsklength);
  1369.         }
  1370.     } else {
  1371.         int result = DISK_ReadMFM (dskpt);
  1372.     }
  1373.     regs.spcflags &= ~SPCFLAG_DISK;
  1374.     INTREQ(0x9002);
  1375.     dskdmaen = -1;
  1376.     }
  1377. }
  1378.  
  1379. static __inline__ void pfield_fetchdata(void)
  1380. {
  1381.     if (dmaen(0x100) && pfield_linedmaon) {
  1382.     switch(bplplanecnt){
  1383.      case 8:
  1384.         bpl8dat = chipmem_bank.wget(bplpt[7]); bplpt[7] += 2; bpl8dat <<= 7;
  1385.      case 7:
  1386.         bpl7dat = chipmem_bank.wget(bplpt[6]); bplpt[6] += 2; bpl7dat <<= 6;
  1387.      case 6:
  1388.         bpl6dat = chipmem_bank.wget(bplpt[5]); bplpt[5] += 2; bpl6dat <<= 5;
  1389.      case 5:
  1390.         bpl5dat = chipmem_bank.wget(bplpt[4]); bplpt[4] += 2; bpl5dat <<= 4;
  1391.      case 4:
  1392.         bpl4dat = chipmem_bank.wget(bplpt[3]); bplpt[3] += 2; bpl4dat <<= 3;
  1393.      case 3:
  1394.         bpl3dat = chipmem_bank.wget(bplpt[2]); bplpt[2] += 2; bpl3dat <<= 2;
  1395.      case 2:
  1396.         bpl2dat = chipmem_bank.wget(bplpt[1]); bplpt[1] += 2; bpl2dat <<= 1;
  1397.      case 1:
  1398.         bpl1dat = chipmem_bank.wget(bplpt[0]); bplpt[0] += 2;
  1399.     }
  1400.     }
  1401. }
  1402.  
  1403. static void do_sprites(int currvp, int currhp)
  1404. {   
  1405.     int i;
  1406.     int maxspr = currhp/4 - 0x18/4;
  1407.     
  1408.     if (currvp == 0)
  1409.     return;
  1410.     if (maxspr < 0)
  1411.     return;
  1412.     if (maxspr > 7)
  1413.     maxspr = 7;
  1414.     
  1415.     for(i = last_sprite; i <= maxspr; i++) {
  1416.     int vstart = (bpl_info.sprpos[i] >> 8) | ((bpl_info.sprctl[i] << 6) & 0x100);
  1417.     int vstop = (bpl_info.sprctl[i] >> 8) | ((bpl_info.sprctl[i] << 7) & 0x100);
  1418.     if ((vstart <= currvp && vstop >= currvp) || spron[i] == 1) {
  1419.         if (dmaen(0x20)) {
  1420.         UWORD data1 = chipmem_bank.wget(sprpt[i]);
  1421.         UWORD data2 = chipmem_bank.wget(sprpt[i]+2);
  1422.         sprpt[i] += 4;
  1423.         
  1424.         if (vstop != currvp && spron[i] != 1) {
  1425.             /* Hack for X mouse auto-calibration */
  1426.             if (i == 0 && !sprvbfl && ((bpl_info.sprpos[0]&0xff)<<2)>0x60) {
  1427.             spr0ctl=bpl_info.sprctl[0];
  1428.             spr0pos=bpl_info.sprpos[0];
  1429.             sprvbfl=2;
  1430.             }
  1431.             SPRxDATB_1(data2, i);
  1432.             SPRxDATA_1(data1, i);
  1433.         } else {
  1434.             SPRxPOS_1(data1, i);
  1435.             SPRxCTL_1(data2, i);
  1436.         }
  1437.         }
  1438.     }
  1439.     }
  1440.     last_sprite = maxspr + 1;
  1441. }
  1442.  
  1443. static __inline__ void pfield_modulos(int add)
  1444. {
  1445.     switch(bplplanecnt){
  1446.      case 8:
  1447.     bplpt[7] += add + bpl2mod;
  1448.      case 7:
  1449.     bplpt[6] += add + bpl1mod;
  1450.      case 6:
  1451.     bplpt[5] += add + bpl2mod;
  1452.      case 5:
  1453.     bplpt[4] += add + bpl1mod;
  1454.      case 4:
  1455.     bplpt[3] += add + bpl2mod;
  1456.      case 3:
  1457.     bplpt[2] += add + bpl1mod;
  1458.      case 2:
  1459.     bplpt[1] += add + bpl2mod;
  1460.      case 1:
  1461.     bplpt[0] += add + bpl1mod;
  1462.     }
  1463. }
  1464.  
  1465. #if AGA_CHIPSET == 0
  1466. static void pfield_sprite (int num, int sprxp, UWORD data, UWORD datb, int lores)
  1467. {
  1468.     int i;
  1469.  
  1470.     int *lookup = bpldualpf ? (bpldualpfpri ? dblpf_ind2 : dblpf_ind1) : linear_map_256;
  1471.     int *lookup_no = bpldualpf ? (bpldualpfpri ? dblpf_2nd2 : dblpf_2nd1) : lots_of_twos;
  1472.  
  1473.     for(i = 15; i >= 0; i--, data >>= 1, datb >>= 1) {
  1474.     int sprxpos = sprxp + i*(lores ? 1 : 2);
  1475.     int plno;
  1476.     int col;
  1477.  
  1478.     /* When doing the slow line update, the following condition can
  1479.      * happen. Lower-numbered sprites get the higher priority. */
  1480.     if (spixstate[sprxpos] & ((1 << num)-1))
  1481.         continue;
  1482.  
  1483.     /* Check the priority, but only if we did not already put a sprite
  1484.      * pixel at this position. If there's already a sprite pixel here, 
  1485.      * the priority was previously tested. */
  1486.     if (!spixstate[sprxpos]) {
  1487.         /* ??? What about hires mode when one hires pixel is 0, enabling the
  1488.          * sprite, and the other is != 0, blocking it? */
  1489.         plno = lookup_no[pixdata.apixels[sprxpos]];
  1490.         if (plno != 0 && (1 << num) >= plfpri[plno])
  1491.         continue;
  1492.     }
  1493.  
  1494.     if ((bpl_info.sprctl[num] & 0x80) && (num & 1)) {
  1495.         /* Attached sprite */
  1496.         col = ((data << 2) & 4) + ((datb << 3) & 8);
  1497.         spixstate[sprxpos] |= 1 << (num-1);
  1498.         spixels[sprxpos] = col;
  1499.     } else {            
  1500.         col = (data & 1) | ((datb << 1) & 2);
  1501.         if (spixstate[sprxpos] & (1 << num)) {
  1502.         /* Finish attached sprite */
  1503.         /* Did the upper half of the sprite have any bits set? */
  1504.         if (spixstate[sprxpos] & (1 << (num+1)))
  1505.             col += spixels[sprxpos];
  1506.         /* Is there any sprite pixel at this position at all? */
  1507.         if (!col) {
  1508.             spixstate[sprxpos] &= ~(3 << num);
  1509.         } else
  1510.             col += 16;
  1511.         } else {
  1512.         if (col) {
  1513.             col |= 16 | ((num & 6) << 1);
  1514.         }
  1515.         }
  1516.     }
  1517.     if (col) {
  1518.         pixdata.apixels[sprxpos] = col;
  1519.         spixstate[sprxpos] |= 1<<num;
  1520.         if (!lores) {
  1521.         pixdata.apixels[sprxpos+1] = col;
  1522.         spixstate[sprxpos+1] |= 1<<num;
  1523.         }
  1524.         if (bplham && bplplanecnt == 6) {
  1525.         ham_linebuf[sprxpos] = bpl_info.color_regs[col];
  1526.         if (!lores)
  1527.             ham_linebuf[sprxpos+1] = bpl_info.color_regs[col];
  1528.         }
  1529.     }
  1530.     }
  1531. }
  1532. #else /* AGA version */
  1533. static void pfield_sprite (int num, int sprxp, UWORD data, UWORD datb, int lores)
  1534. {
  1535.     int i;
  1536.  
  1537.     int *lookup = bpldualpf ? (bpldualpfpri ? dblpf_ind2 : dblpf_ind1) : linear_map_256;
  1538.     int *lookup_no = bpldualpf ? (bpldualpfpri ? dblpf_2nd2 : dblpf_2nd1) : lots_of_twos;
  1539.  
  1540.     for(i = 15; i >= 0; i--, data >>= 1, datb >>= 1) {
  1541.     int sprxpos = sprxp + i*(lores ? 1 : 2);
  1542.     int plno;
  1543.     int col;
  1544.  
  1545.     /* When doing the slow line update, the following condition can
  1546.      * happen. Lower-numbered sprites get the higher priority. */
  1547.     if (spixstate[sprxpos] & ((1 << num)-1))
  1548.         continue;
  1549.  
  1550.     /* Check the priority, but only if we did not already put a sprite
  1551.      * pixel at this position. If there's already a sprite pixel here, 
  1552.      * the priority was previously tested. */
  1553.     if (!spixstate[sprxpos]) {
  1554.         /* ??? What about hires mode when one hires pixel is 0, enabling the
  1555.          * sprite, and the other is != 0, blocking it? */
  1556.         plno = lookup_no[pixdata.apixels[sprxpos]];
  1557.         if (plno != 0 && (1 << num) >= plfpri[plno])
  1558.         continue;
  1559.     }
  1560.  
  1561.     if ((bpl_info.sprctl[num] & 0x80) && (num & 1)) {
  1562.         /* Attached sprite */
  1563.         col = ((data << 2) & 4) + ((datb << 3) & 8);
  1564.         spixstate[sprxpos] |= 1 << (num-1);
  1565.         spixels[sprxpos] = col;
  1566.     } else {            
  1567.         col = (data & 1) | ((datb << 1) & 2);
  1568.         if (spixstate[sprxpos] & (1 << num)) {
  1569.         /* Finish attached sprite */
  1570.         /* Did the upper half of the sprite have any bits set? */
  1571.         if (spixstate[sprxpos] & (1 << (num+1)))
  1572.             col += spixels[sprxpos];
  1573.         /* Is there any sprite pixel at this position at all? */
  1574.         if (!col) {
  1575.             spixstate[sprxpos] &= ~(3 << num);
  1576.         } else
  1577.             col += ((bpl_info.bplcon4 << (num & 1 ? 4 : 0)) & 240);
  1578.         } else {
  1579.         if (col) {
  1580.             col |= ((bpl_info.bplcon4 << (num & 1 ? 4 : 0)) & 240) | ((num & 6) << 1);
  1581.         }
  1582.         }
  1583.     }
  1584.     if (col) {
  1585.         pixdata.apixels[sprxpos] = col;
  1586.         spixstate[sprxpos] |= 1<<num;
  1587.         if (!lores) {
  1588.         pixdata.apixels[sprxpos+1] = col;
  1589.         spixstate[sprxpos+1] |= 1<<num;
  1590.         }
  1591.         if (bplham && bplplanecnt == 6) {
  1592.         ham_linebuf[sprxpos] = bpl_info.color_regs[col];
  1593.         if (!lores)
  1594.             ham_linebuf[sprxpos+1] = bpl_info.color_regs[col];
  1595.         }
  1596.     }
  1597.     }
  1598. }
  1599. #endif
  1600.  
  1601. static __inline__ UBYTE *pfield_xlateptr(CPTR plpt, int bytecount)
  1602. {
  1603.     if (!chipmem_bank.check(plpt,bytecount)) {
  1604.     static int count = 0;
  1605.     if (count < 5) {
  1606.         count++;
  1607.         fprintf(stderr, "Warning: Bad playfield pointer");
  1608.         if (count == 5) fprintf(stderr, " (no further warnings)");
  1609.         fprintf(stderr, "\n");
  1610.     }
  1611.     return NULL;
  1612.     }
  1613.     return chipmem_bank.xlateaddr(plpt);
  1614. }
  1615.  
  1616. static void pfield_doline_slow_h(int currhpos)
  1617. {
  1618.     int xpos = currhpos * 4 - 0x60;
  1619.         
  1620.     if (bplhires) {
  1621.     int offs1 = xpos + 16 + bpldelay1*2;
  1622.     int offs2 = xpos + 16 + bpldelay2*2;
  1623.     
  1624.     int pix;
  1625.     for(pix = 15; pix >= 0; pix--) {
  1626.         switch(bplplanecnt) {
  1627.          case 8:
  1628.         pixdata.apixels[pix + offs2] |= bpl8dat & 0x80; bpl8dat >>= 1;
  1629.          case 7:
  1630.         pixdata.apixels[pix + offs1] |= bpl7dat & 0x40; bpl7dat >>= 1;
  1631.          case 6:
  1632.         pixdata.apixels[pix + offs2] |= bpl6dat & 0x20; bpl6dat >>= 1;
  1633.          case 5:
  1634.         pixdata.apixels[pix + offs1] |= bpl5dat & 0x10; bpl5dat >>= 1;
  1635.          case 4:
  1636.         pixdata.apixels[pix + offs2] |= bpl4dat & 0x8; bpl4dat >>= 1;
  1637.          case 3:
  1638.         pixdata.apixels[pix + offs1] |= bpl3dat & 0x4; bpl3dat >>= 1;
  1639.          case 2:
  1640.         pixdata.apixels[pix + offs2] |= bpl2dat & 0x2; bpl2dat >>= 1;
  1641.          case 1:
  1642.         pixdata.apixels[pix + offs1] |= bpl1dat & 0x1; bpl1dat >>= 1;
  1643.         }
  1644.     }
  1645.     } else {
  1646.     int offs1 = xpos + 32 + bpldelay1*2;
  1647.     int offs2 = xpos + 32 + bpldelay2*2;
  1648.     
  1649.     int pix;
  1650.     for(pix = 30; pix >= 0; pix -= 2) {
  1651.         switch(bplplanecnt) {
  1652.          case 8:
  1653.         pixdata.apixels[pix + offs2] |= bpl8dat & 0x80;
  1654.         pixdata.apixels[pix + offs2 + 1] |= bpl8dat & 0x80; bpl8dat >>= 1;
  1655.          case 7:
  1656.         pixdata.apixels[pix + offs1] |= bpl7dat & 0x40; 
  1657.         pixdata.apixels[pix + offs1 + 1] |= bpl7dat & 0x40; bpl7dat >>= 1;
  1658.          case 6:
  1659.         pixdata.apixels[pix + offs2] |= bpl6dat & 0x20;
  1660.         pixdata.apixels[pix + offs2 + 1] |= bpl6dat & 0x20; bpl6dat >>= 1;
  1661.          case 5:
  1662.         pixdata.apixels[pix + offs1] |= bpl5dat & 0x10;
  1663.         pixdata.apixels[pix + offs1 + 1] |= bpl5dat & 0x10; bpl5dat >>= 1;
  1664.          case 4:
  1665.         pixdata.apixels[pix + offs2] |= bpl4dat & 0x8;
  1666.         pixdata.apixels[pix + offs2 + 1] |= bpl4dat & 0x8; bpl4dat >>= 1;
  1667.          case 3:
  1668.         pixdata.apixels[pix + offs1] |= bpl3dat & 0x4; 
  1669.         pixdata.apixels[pix + offs1 + 1] |= bpl3dat & 0x4; bpl3dat >>= 1;
  1670.          case 2:
  1671.         pixdata.apixels[pix + offs2] |= bpl2dat & 0x2; 
  1672.         pixdata.apixels[pix + offs2 + 1] |= bpl2dat & 0x2; bpl2dat >>= 1;
  1673.          case 1:
  1674.         pixdata.apixels[pix + offs1] |= bpl1dat & 0x1; 
  1675.         pixdata.apixels[pix + offs1 + 1] |= bpl1dat & 0x1; bpl1dat >>= 1;
  1676.         }
  1677.     }
  1678.     }    
  1679. }
  1680.  
  1681. static void pfield_doline_slow_l(int currhpos)
  1682. {
  1683.     int xpos = currhpos * 2 - 0x30;
  1684.     
  1685.     if (bplhires) {
  1686.     int offs1 = xpos + 8 + bpldelay1;
  1687.     int offs2 = xpos + 8 + bpldelay2;
  1688.     
  1689.     int pix;
  1690.     for(pix = 7; pix >= 0; pix--) {
  1691.         switch(bplplanecnt) {
  1692.          case 8:
  1693.         pixdata.apixels[pix + offs2] |= bpl8dat & 0x80; bpl8dat >>= 2;
  1694.          case 7:
  1695.         pixdata.apixels[pix + offs1] |= bpl7dat & 0x40; bpl7dat >>= 2;
  1696.          case 6:
  1697.         pixdata.apixels[pix + offs2] |= bpl6dat & 0x20; bpl6dat >>= 2;
  1698.          case 5:
  1699.         pixdata.apixels[pix + offs1] |= bpl5dat & 0x10; bpl5dat >>= 2;
  1700.          case 4:
  1701.         pixdata.apixels[pix + offs2] |= bpl4dat & 0x8; bpl4dat >>= 2;
  1702.          case 3:
  1703.         pixdata.apixels[pix + offs1] |= bpl3dat & 0x4; bpl3dat >>= 2;
  1704.          case 2:
  1705.         pixdata.apixels[pix + offs2] |= bpl2dat & 0x2; bpl2dat >>= 2;
  1706.          case 1:
  1707.         pixdata.apixels[pix + offs1] |= bpl1dat & 0x1; bpl1dat >>= 2;
  1708.         }
  1709.     }
  1710.     } else {
  1711.     int offs1 = xpos + 16 + bpldelay1;
  1712.     int offs2 = xpos + 16 + bpldelay2;
  1713.     
  1714.     int pix;
  1715.     for(pix = 15; pix >= 0; pix --) {
  1716.         switch(bplplanecnt) {
  1717.          case 8:
  1718.         pixdata.apixels[pix + offs2] |= bpl8dat & 0x80; bpl8dat >>= 1;
  1719.          case 7:
  1720.         pixdata.apixels[pix + offs1] |= bpl7dat & 0x40; bpl7dat >>= 1;
  1721.          case 6:
  1722.         pixdata.apixels[pix + offs2] |= bpl6dat & 0x20; bpl6dat >>= 1;
  1723.          case 5:
  1724.         pixdata.apixels[pix + offs1] |= bpl5dat & 0x10; bpl5dat >>= 1;
  1725.          case 4:
  1726.         pixdata.apixels[pix + offs2] |= bpl4dat & 0x8; bpl4dat >>= 1;
  1727.          case 3:
  1728.         pixdata.apixels[pix + offs1] |= bpl3dat & 0x4; bpl3dat >>= 1;
  1729.          case 2:
  1730.         pixdata.apixels[pix + offs2] |= bpl2dat & 0x2; bpl2dat >>= 1;
  1731.          case 1:
  1732.         pixdata.apixels[pix + offs1] |= bpl1dat & 0x1; bpl1dat >>= 1;
  1733.         }
  1734.     }
  1735.     }    
  1736. }
  1737.  
  1738. ULONG hirestab_h[256][2];
  1739. ULONG lorestab_h[256][4];
  1740.  
  1741. ULONG hirestab_l[256][1];
  1742. ULONG lorestab_l[256][2];
  1743.  
  1744. static void gen_pfield_tables(void)
  1745. {
  1746.     int i;
  1747.     union {
  1748.     struct {
  1749.         UBYTE a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p;
  1750.     } foo;
  1751.     struct {
  1752.         ULONG a, b, c, d;
  1753.     } bar;
  1754.     } baz;
  1755.     
  1756.     for (i = 0; i < 256; i++) {
  1757.     /* We lose every second pixel in HiRes if UAE runs in a 320x200 screen. */
  1758.     baz.foo.a = i & 64 ? 1 : 0;
  1759.     baz.foo.b = i & 16 ? 1 : 0;
  1760.     baz.foo.c = i & 4 ? 1 : 0;
  1761.     baz.foo.d = i & 1 ? 1 : 0;
  1762.     hirestab_l[i][0] = baz.bar.a;
  1763.     
  1764.     baz.foo.a = i & 128 ? 1 : 0;
  1765.     baz.foo.b = i & 64 ? 1 : 0;
  1766.     baz.foo.c = i & 32 ? 1 : 0;
  1767.     baz.foo.d = i & 16 ? 1 : 0;
  1768.     baz.foo.e = i & 8 ? 1 : 0;
  1769.     baz.foo.f = i & 4 ? 1 : 0;
  1770.     baz.foo.g = i & 2 ? 1 : 0;
  1771.     baz.foo.h = i & 1 ? 1 : 0;
  1772.     lorestab_l[i][0] = baz.bar.a;
  1773.     lorestab_l[i][1] = baz.bar.b;
  1774.     }
  1775.     
  1776.     for (i = 0; i < 256; i++) {
  1777.     baz.foo.a = i & 128 ? 1 : 0;
  1778.     baz.foo.b = i & 64 ? 1 : 0;
  1779.     baz.foo.c = i & 32 ? 1 : 0;
  1780.     baz.foo.d = i & 16 ? 1 : 0;
  1781.     baz.foo.e = i & 8 ? 1 : 0;
  1782.     baz.foo.f = i & 4 ? 1 : 0;
  1783.     baz.foo.g = i & 2 ? 1 : 0;
  1784.     baz.foo.h = i & 1 ? 1 : 0;
  1785.     hirestab_h[i][0] = baz.bar.a;
  1786.     hirestab_h[i][1] = baz.bar.b;
  1787.     
  1788.     baz.foo.a = i & 128 ? 1 : 0;
  1789.     baz.foo.b = i & 128 ? 1 : 0;
  1790.     baz.foo.c = i & 64 ? 1 : 0;
  1791.     baz.foo.d = i & 64 ? 1 : 0;
  1792.     baz.foo.e = i & 32 ? 1 : 0;
  1793.     baz.foo.f = i & 32 ? 1 : 0;
  1794.     baz.foo.g = i & 16 ? 1 : 0;
  1795.     baz.foo.h = i & 16 ? 1 : 0;
  1796.     baz.foo.i = i & 8 ? 1 : 0;
  1797.     baz.foo.j = i & 8 ? 1 : 0;
  1798.     baz.foo.k = i & 4 ? 1 : 0;
  1799.     baz.foo.l = i & 4 ? 1 : 0;
  1800.     baz.foo.m = i & 2 ? 1 : 0;
  1801.     baz.foo.n = i & 2 ? 1 : 0;
  1802.     baz.foo.o = i & 1 ? 1 : 0;
  1803.     baz.foo.p = i & 1 ? 1 : 0;
  1804.     lorestab_h[i][0] = baz.bar.a;
  1805.     lorestab_h[i][1] = baz.bar.b;
  1806.     lorestab_h[i][2] = baz.bar.c;
  1807.     lorestab_h[i][3] = baz.bar.d;
  1808.     }
  1809. }
  1810.  
  1811. static __inline__ void pfield_orword_hires_h(int data, unsigned char *dp, int bit)
  1812. {
  1813.     ULONG *pixptr = (ULONG *)dp;
  1814.  
  1815.     *pixptr |= hirestab_h[data >> 8][0] << bit;
  1816.     *(pixptr+1) |= hirestab_h[data >> 8][1] << bit;
  1817.     *(pixptr+2) |= hirestab_h[data & 255][0] << bit;
  1818.     *(pixptr+3) |= hirestab_h[data & 255][1] << bit;
  1819. }
  1820.  
  1821. static __inline__ void pfield_orword_lores_h(int data, unsigned char *dp, int bit)
  1822. {
  1823.     ULONG *pixptr = (ULONG *)dp;
  1824.     
  1825.     *pixptr |= lorestab_h[data >> 8][0] << bit;
  1826.     *(pixptr+1) |= lorestab_h[data >> 8][1] << bit;
  1827.     *(pixptr+2) |= lorestab_h[data >> 8][2] << bit;
  1828.     *(pixptr+3) |= lorestab_h[data >> 8][3] << bit;
  1829.     *(pixptr+4) |= lorestab_h[data & 255][0] << bit;
  1830.     *(pixptr+5) |= lorestab_h[data & 255][1] << bit;
  1831.     *(pixptr+6) |= lorestab_h[data & 255][2] << bit;
  1832.     *(pixptr+7) |= lorestab_h[data & 255][3] << bit;
  1833. }
  1834.  
  1835. static __inline__ void pfield_setword_hires_h(int data, unsigned char *dp, int bit)
  1836. {
  1837.     ULONG *pixptr = (ULONG *)dp;
  1838.  
  1839.     *pixptr = hirestab_h[data >> 8][0] << bit;
  1840.     *(pixptr+1) = hirestab_h[data >> 8][1] << bit;
  1841.     *(pixptr+2) = hirestab_h[data & 255][0] << bit;
  1842.     *(pixptr+3) = hirestab_h[data & 255][1] << bit;
  1843. }
  1844.  
  1845. static __inline__ void pfield_setword_lores_h(int data, unsigned char *dp, int bit)
  1846. {
  1847.     ULONG *pixptr = (ULONG *)dp;
  1848.     
  1849.     *pixptr = lorestab_h[data >> 8][0] << bit;
  1850.     *(pixptr+1) = lorestab_h[data >> 8][1] << bit;
  1851.     *(pixptr+2) = lorestab_h[data >> 8][2] << bit;
  1852.     *(pixptr+3) = lorestab_h[data >> 8][3] << bit;
  1853.     *(pixptr+4) = lorestab_h[data & 255][0] << bit;
  1854.     *(pixptr+5) = lorestab_h[data & 255][1] << bit;
  1855.     *(pixptr+6) = lorestab_h[data & 255][2] << bit;
  1856.     *(pixptr+7) = lorestab_h[data & 255][3] << bit;
  1857. }
  1858.  
  1859. static __inline__ void pfield_orword_hires_l(int data, unsigned char *dp, int bit)
  1860. {
  1861.     ULONG *pixptr = (ULONG *)dp;
  1862.  
  1863.     *pixptr |= hirestab_l[data >> 8][0] << bit;
  1864.     *(pixptr+1) |= hirestab_l[data & 255][0] << bit;
  1865. }
  1866.  
  1867. static __inline__ void pfield_orword_lores_l(int data, unsigned char *dp, int bit)
  1868. {
  1869.     ULONG *pixptr = (ULONG *)dp;
  1870.     
  1871.     *pixptr |= lorestab_l[data >> 8][0] << bit;
  1872.     *(pixptr+1) |= lorestab_l[data >> 8][1] << bit;
  1873.     *(pixptr+2) |= lorestab_l[data & 255][0] << bit;
  1874.     *(pixptr+3) |= lorestab_l[data & 255][1] << bit;
  1875. }
  1876.  
  1877. static __inline__ void pfield_setword_hires_l(int data, unsigned char *dp, int bit)
  1878. {
  1879.     ULONG *pixptr = (ULONG *)dp;
  1880.  
  1881.     *pixptr = hirestab_l[data >> 8][0] << bit;
  1882.     *(pixptr+1) = hirestab_l[data & 255][0] << bit;
  1883. }
  1884.  
  1885. static __inline__ void pfield_setword_lores_l(int data, unsigned char *dp, int bit)
  1886. {
  1887.     ULONG *pixptr = (ULONG *)dp;
  1888.     
  1889.     *pixptr = lorestab_l[data >> 8][0] << bit;
  1890.     *(pixptr+1) = lorestab_l[data >> 8][1] << bit;
  1891.     *(pixptr+2) = lorestab_l[data & 255][0] << bit;
  1892.     *(pixptr+3) = lorestab_l[data & 255][1] << bit;
  1893. }
  1894.  
  1895. #define DO_ONE_PLANE(POINTER, MULT, FUNC, DELAY, LL_SUB, P_ADD) { \
  1896.     int i; \
  1897.     unsigned int bpldat1; \
  1898.     UWORD data; \
  1899.     unsigned int bpldat2 = 0; \
  1900.     for (i = plflinelen; i > 0; i -= LL_SUB) { \
  1901.         bpldat1 = bpldat2; \
  1902.     bpldat2 = (*POINTER << 8) | *(POINTER+1); \
  1903.         POINTER+=2; \
  1904.     data = (bpldat1 << (16 - DELAY)) | (bpldat2 >> DELAY); \
  1905.     FUNC(data, app, MULT); \
  1906.     app += P_ADD; \
  1907.     } \
  1908.     data = bpldat2 << (16 - DELAY); \
  1909.     FUNC(data, app, MULT); \
  1910. }
  1911.  
  1912. #if 1
  1913. static void pfield_doline_h(void)
  1914. {
  1915.     int xpos = plfstrt * 4 - 0x60;
  1916.     int spr, sprites_seen;
  1917.  
  1918.     if (bplhires) {
  1919.     if (bplplanecnt > 0) {
  1920.         int xpos1 = xpos + 16 + (bpldelay1 >= 8 ? 16 : 0);
  1921.         int xpos2 = xpos + 16 + (bpldelay2 >= 8 ? 16 : 0);
  1922.         int delay1 = 2*(bpldelay1 & 7);
  1923.         int delay2 = 2*(bpldelay2 & 7);
  1924.         unsigned char *app = pixdata.apixels + xpos1;
  1925.  
  1926.         DO_ONE_PLANE(r_bplpt[0], 0, pfield_setword_hires_h, delay1, 4, 16);
  1927.         if (bplplanecnt > 2) {
  1928.         app = pixdata.apixels + xpos1;
  1929.         DO_ONE_PLANE(r_bplpt[2], 2, pfield_orword_hires_h, delay1, 4, 16);
  1930.         }
  1931. #if AGA_CHIPSET == 1
  1932.         if (bplplanecnt > 4) {
  1933.         app = pixdata.apixels + xpos1;
  1934.         DO_ONE_PLANE(r_bplpt[4], 4, pfield_orword_hires_h, delay1, 4, 16);
  1935.         }
  1936.         if (bplplanecnt > 6) {
  1937.         app = pixdata.apixels + xpos1;
  1938.         DO_ONE_PLANE(r_bplpt[6], 6, pfield_orword_hires_h, delay1, 4, 16);
  1939.         }
  1940. #endif
  1941.         if (bplplanecnt > 1) {
  1942.         app = pixdata.apixels + xpos2;
  1943.         DO_ONE_PLANE(r_bplpt[1], 1, pfield_orword_hires_h, delay2, 4, 16);
  1944.         }
  1945.         if (bplplanecnt > 3) {
  1946.         app = pixdata.apixels + xpos2;
  1947.         DO_ONE_PLANE(r_bplpt[3], 3, pfield_orword_hires_h, delay2, 4, 16);
  1948.         }
  1949. #if AGA_CHIPSET == 1
  1950.         if (bplplanecnt > 5) {
  1951.         app = pixdata.apixels + xpos2;
  1952.         DO_ONE_PLANE(r_bplpt[5], 5, pfield_orword_hires_h, delay2, 4, 16);
  1953.         }
  1954.         if (bplplanecnt > 7) {
  1955.         app = pixdata.apixels + xpos2;
  1956.         DO_ONE_PLANE(r_bplpt[7], 7, pfield_orword_hires_h, delay2, 4, 16);
  1957.         }
  1958. #endif
  1959.     } else {
  1960.         memset(pixdata.apixels, 0, sizeof(pixdata.apixels));
  1961.     }
  1962.     } else {
  1963.     if (bplplanecnt > 0) {
  1964.         int x = xpos + 32;
  1965.         unsigned char *app = pixdata.apixels + x;
  1966.         
  1967.         DO_ONE_PLANE(r_bplpt[0], 0, pfield_setword_lores_h, bpldelay1, 8, 32);
  1968.         if (bplplanecnt > 2) {
  1969.         app = pixdata.apixels + x;
  1970.         DO_ONE_PLANE(r_bplpt[2], 2, pfield_orword_lores_h, bpldelay1, 8, 32);
  1971.         }
  1972.         if (bplplanecnt > 4) {
  1973.         app = pixdata.apixels + x;
  1974.         DO_ONE_PLANE(r_bplpt[4], 4, pfield_orword_lores_h, bpldelay1, 8, 32);
  1975.         }
  1976. #if AGA_CHIPSET == 1
  1977.         if (bplplanecnt > 6) {
  1978.         app = pixdata.apixels + x;
  1979.         DO_ONE_PLANE(r_bplpt[6], 6, pfield_orword_lores_h, bpldelay1, 8, 32);
  1980.         }
  1981. #endif
  1982.         if (bplplanecnt > 1) {
  1983.         app = pixdata.apixels + x;
  1984.         DO_ONE_PLANE(r_bplpt[1], 1, pfield_orword_lores_h, bpldelay2, 8, 32);
  1985.         }
  1986.         if (bplplanecnt > 3) {
  1987.         app = pixdata.apixels + x;
  1988.         DO_ONE_PLANE(r_bplpt[3], 3, pfield_orword_lores_h, bpldelay2, 8, 32);
  1989.         }
  1990.         if (bplplanecnt > 5) {
  1991.         app = pixdata.apixels + x;
  1992.         DO_ONE_PLANE(r_bplpt[5], 5, pfield_orword_lores_h, bpldelay2, 8, 32);
  1993.         }
  1994. #if AGA_CHIPSET == 1
  1995.         if (bplplanecnt > 7) {
  1996.         app = pixdata.apixels + x;
  1997.         DO_ONE_PLANE(r_bplpt[7], 7, pfield_orword_lores_h, bpldelay2, 8, 32);
  1998.         }
  1999. #endif
  2000.     } else {
  2001.         memset(pixdata.apixels, 0, sizeof(pixdata.apixels));
  2002.     }
  2003.     }
  2004.     
  2005.     decode_ham6 (16,812);
  2006.         
  2007.     linetoscreen = 1;
  2008.     sprites_seen = 0;
  2009.     
  2010.     for(spr = 7; spr >= 0; spr--) {
  2011.     if (bpl_info.sprarmed[spr]) {
  2012.         int sprxp = ((bpl_info.sprpos[spr] & 0xFF) * 4) - 0x60 + (bpl_info.sprctl[spr] & 1)*2;
  2013.         int i;
  2014.         /* Ugh. Nasty bug. Let's rather lose some sprites than trash
  2015.          * memory. */
  2016.         if (sprxp >= 0) {
  2017.         if (!sprites_seen) {
  2018.             sprites_seen = 1;
  2019.             memset(spixstate,0,sizeof spixstate);
  2020.         }
  2021.         pfield_sprite (spr, sprxp, bpl_info.sprdata[spr], bpl_info.sprdatb[spr], 0);
  2022.         }
  2023.     }
  2024.     }
  2025. }
  2026. #endif
  2027. static void pfield_doline_l(void)
  2028. {
  2029.     int xpos = plfstrt * 2 - 0x30;
  2030.     int spr;
  2031.     int sprites_seen;
  2032.         
  2033.     if (bplhires) {
  2034.     if (bplplanecnt > 0) {
  2035.         int xpos1 = xpos + 8 + (bpldelay1 >= 8 ? 8 : 0);
  2036.         int xpos2 = xpos + 8 + (bpldelay2 >= 8 ? 8 : 0);
  2037.         int delay1 = (bpldelay1 & 7) * 2;
  2038.         int delay2 = (bpldelay2 & 7) * 2;
  2039.         unsigned char *app = pixdata.apixels + xpos1;
  2040.         
  2041.         DO_ONE_PLANE(r_bplpt[0], 0, pfield_setword_hires_l, delay1, 4, 8);
  2042.         if (bplplanecnt > 2) {
  2043.         app = pixdata.apixels + xpos1;
  2044.         DO_ONE_PLANE(r_bplpt[2], 2, pfield_orword_hires_l, delay1, 4, 8);
  2045.         }
  2046. #if AGA_CHIPSET == 1
  2047.         if (bplplanecnt > 4) {
  2048.         app = pixdata.apixels + xpos1;
  2049.         DO_ONE_PLANE(r_bplpt[4], 4, pfield_orword_hires_l, delay1, 4, 8);
  2050.         }
  2051.         if (bplplanecnt > 6) {
  2052.         app = pixdata.apixels + xpos1;
  2053.         DO_ONE_PLANE(r_bplpt[6], 6, pfield_orword_hires_l, delay1, 4, 8);
  2054.         }
  2055. #endif
  2056.         if (bplplanecnt > 1) {
  2057.         app = pixdata.apixels + xpos2;
  2058.         DO_ONE_PLANE(r_bplpt[1], 1, pfield_orword_hires_l, delay2, 4, 8);
  2059.         }
  2060.         if (bplplanecnt > 3) {
  2061.         app = pixdata.apixels + xpos2;
  2062.         DO_ONE_PLANE(r_bplpt[3], 3, pfield_orword_hires_l, delay2, 4, 8);
  2063.         }
  2064. #if AGA_CHIPSET == 1
  2065.         if (bplplanecnt > 5) {
  2066.         app = pixdata.apixels + xpos2;
  2067.         DO_ONE_PLANE(r_bplpt[5], 5, pfield_orword_hires_l, delay2, 4, 8);
  2068.         }
  2069.         if (bplplanecnt > 7) {
  2070.         app = pixdata.apixels + xpos2;
  2071.         DO_ONE_PLANE(r_bplpt[7], 7, pfield_orword_hires_l, delay2, 4, 8);
  2072.         }
  2073. #endif
  2074.     } else {
  2075.         memset(pixdata.apixels, 0, sizeof(pixdata.apixels));
  2076.     }
  2077.     } else {
  2078.     if (bplplanecnt > 0) {
  2079.         int x = xpos + 16;
  2080.         int delay1 = bpldelay1;
  2081.         int delay2 = bpldelay2;
  2082.         unsigned char *app = pixdata.apixels + x;
  2083.         DO_ONE_PLANE(r_bplpt[0], 0, pfield_setword_lores_l, delay1, 8, 16);
  2084.         if (bplplanecnt > 2) {
  2085.         app = pixdata.apixels + x;
  2086.         DO_ONE_PLANE(r_bplpt[2], 2, pfield_orword_lores_l, delay1, 8, 16);
  2087.         }
  2088.         if (bplplanecnt > 4) {
  2089.         app = pixdata.apixels + x;
  2090.         DO_ONE_PLANE(r_bplpt[4], 4, pfield_orword_lores_l, delay1, 8, 16);
  2091.         }
  2092. #if AGA_CHIPSET == 1
  2093.         if (bplplanecnt > 6) {
  2094.         app = pixdata.apixels + x;
  2095.         DO_ONE_PLANE(r_bplpt[6], 6, pfield_orword_lores_l, delay1, 8, 16);
  2096.         }
  2097. #endif
  2098.         if (bplplanecnt > 1) {
  2099.         app = pixdata.apixels + x;
  2100.         DO_ONE_PLANE(r_bplpt[1], 1, pfield_orword_lores_l, delay2, 8, 16);
  2101.         }
  2102.         if (bplplanecnt > 3) {
  2103.         app = pixdata.apixels + x;
  2104.         DO_ONE_PLANE(r_bplpt[3], 3, pfield_orword_lores_l, delay2, 8, 16);
  2105.         }
  2106.         if (bplplanecnt > 5) {
  2107.         app = pixdata.apixels + x;
  2108.         DO_ONE_PLANE(r_bplpt[5], 5, pfield_orword_lores_l, delay2, 8, 16);
  2109.         }
  2110. #if AGA_CHIPSET == 1
  2111.         if (bplplanecnt > 7) {
  2112.         app = pixdata.apixels + x;
  2113.         DO_ONE_PLANE(r_bplpt[7], 7, pfield_orword_lores_l, delay2, 8, 16);
  2114.         }
  2115. #endif
  2116.     } else {
  2117.         memset(pixdata.apixels, 0, sizeof(pixdata.apixels));
  2118.     }
  2119.     }
  2120.  
  2121.     decode_ham6 (8,406);
  2122.  
  2123.     linetoscreen = 1;
  2124.     sprites_seen = 0;
  2125.  
  2126.     for(spr = 7; spr >= 0; spr--) {
  2127.     if (bpl_info.sprarmed[spr]) {
  2128.         int sprxp = ((bpl_info.sprpos[spr] & 0xFF) * 2) - 0x30 + (bpl_info.sprctl[spr] & 1);
  2129.         int i;
  2130.         /* Ugh. Nasty bug. Let's rather lose some sprites than trash
  2131.          * memory. */
  2132.         if (sprxp >= 0) {
  2133.         if (!sprites_seen) {
  2134.             sprites_seen = 1;
  2135.             memset(spixstate,0,sizeof spixstate);
  2136.         }
  2137.             pfield_sprite (spr, sprxp, bpl_info.sprdata[spr], bpl_info.sprdatb[spr], 1);
  2138.         }
  2139.     }
  2140.     }
  2141. }
  2142.  
  2143. static int bpl_data_differs(UBYTE *data, UBYTE *r_addr, int nbytes)
  2144. {
  2145.     return memcmpy(data, r_addr, nbytes);
  2146. }
  2147.  
  2148. static __inline__ int bplinfo_differs(struct bplinfo *a, struct bplinfo *b)
  2149. {
  2150.     int ncolors;
  2151.     int i;
  2152.     if (a->bplcon0 != b->bplcon0
  2153.     || a->bplcon1 != b->bplcon1
  2154.     || a->bplcon2 != b->bplcon2
  2155.     || a->bplcon3 != b->bplcon3
  2156.     || a->bplcon4 != b->bplcon4
  2157.     || a->diwstrt != b->diwstrt
  2158.     || a->diwstop != b->diwstop
  2159.     || a->ddfstrt != b->ddfstrt
  2160.     || a->ddfstop != b->ddfstop)
  2161.     return 1;
  2162.  
  2163. #if 1
  2164.     /* This sometimes loses for sprite colors */
  2165.     ncolors = 1 << ((a->bplcon0 & 0x7000) >> 12);
  2166.     if (ncolors == 64)
  2167.     ncolors = (a->bplcon0 & 0x800) ? 16 : 32;
  2168.     for (i = 0; i < ncolors; i++)
  2169.     if (a->color_regs[i] != b->color_regs[i])
  2170.         return 1;
  2171. #else
  2172.     /* ... and this will lose badly on few-color screens when we implement 
  2173.      * AGA.
  2174.      */
  2175.     if (memcmp(a->color_regs, b->color_regs, sizeof a->color_regs) != 0)
  2176.     return 1;
  2177. #endif
  2178.     for (i = 0; i < 8; i++) {
  2179.     if (a->sprarmed[i] != b->sprarmed[i])
  2180.         return 1;
  2181.     if (a->sprarmed[i]
  2182.         && (a->sprctl[i] != b->sprctl[i]
  2183.         || a->sprpos[i] != b->sprpos[i]
  2184.         || a->sprdata[i] != b->sprdata[i]
  2185.         || a->sprdatb[i] != b->sprdatb[i]))
  2186.         return 1;
  2187.     }
  2188.     return 0;
  2189. }
  2190.  
  2191. static void pfield_doline(void)
  2192. {
  2193.     int bytecount = plflinelen / (bplhires ? 4 : 8) * 2;
  2194.     int drawit = 0;
  2195.     int i;
  2196.     
  2197.     if (vpos < plffirstline || vpos >= plflastline) 
  2198.         return;
  2199.  
  2200.     if (!dmaen(0x100) || !pfield_linedmaon) {
  2201.     line_in_border = 1;
  2202.     return;
  2203.     }
  2204.     
  2205.     for (i = 0; i < bplplanecnt; i++) {
  2206.     r_bplpt[i] = pfield_xlateptr(bplpt[i], bytecount);
  2207.     if (r_bplpt[i] == NULL)
  2208.         return;
  2209.     }
  2210.     
  2211. #if SMART_UPDATE == 1
  2212.     drawit = bplinfo_differs(&bpl_info, &linedescr[next_lineno].bpl_info);
  2213.     if (drawit) {
  2214.     linedescr[next_lineno].bpl_info = bpl_info;
  2215.     }
  2216.     drawit |= !linedescr[next_lineno].linedata_valid | frame_redraw_necessary;
  2217.     
  2218.     if (bytecount <= MAX_WORDS_PER_LINE * 2) {
  2219.     linedescr[next_lineno].linedata_valid = 1;
  2220.     for (i = 0; i < bplplanecnt; i++)
  2221.         drawit |= bpl_data_differs(line_data[next_lineno][i],r_bplpt[i],
  2222.                        bytecount);
  2223.     
  2224.     } else {
  2225.     linedescr[next_lineno].linedata_valid = 0;
  2226.     }
  2227. #endif
  2228.  
  2229.     pfield_modulos(bytecount);
  2230.  
  2231. #if SMART_UPDATE != 0
  2232.     if (!drawit)
  2233.     return;
  2234. #endif    
  2235.  
  2236.     if (use_lores)
  2237.     pfield_doline_l();
  2238.     else
  2239.     pfield_doline_h();
  2240. }
  2241.  
  2242. static void pfield_doline_slow(int currhp)
  2243. {
  2244.     int xpos = PIXEL_XPOS(currhp);
  2245.  
  2246.     if (vpos < plffirstline || vpos >= plflastline) 
  2247.         return;
  2248.     
  2249.     if (currhp == plfstrt)
  2250.     slowline_nextpos = currhp;
  2251.  
  2252.     if (currhp == slowline_nextpos) {    
  2253.     if (slowline_linepos >= plflinelen) {
  2254.         /* The modulos must get added at exactly this point. */
  2255.         pfield_modulos(0);
  2256.         slowline_nextpos = -1;
  2257.     } else {
  2258.         slowline_nextpos += bplhires ? 4 : 8;
  2259.         slowline_linepos += bplhires ? 4 : 8;
  2260.  
  2261.         /* Hmmmm.....
  2262.          * In theory, we could use the fast pfield_doline functions even
  2263.          * in this case. We only need to do sprites, ham decoding and
  2264.          * the line_to_scr stuff cycle-per-cycle. However, we would no
  2265.          * longer be able to emulate tricks that switch lores/hires in
  2266.          * the middle of a line, but I'm not sure whether this sort of
  2267.          * thing actually works currently and whether it's worthwhile.
  2268.          */
  2269.                      
  2270.         pfield_fetchdata();
  2271.  
  2272.         if (use_lores)
  2273.         pfield_doline_slow_l (currhp);
  2274.         else
  2275.         pfield_doline_slow_h (currhp);
  2276.         /* @@@ need to test this */
  2277.         decode_ham6 (xpos, PIXEL_XPOS(slowline_nextpos));
  2278.     }
  2279.     }
  2280.     if (currhp > 48) {
  2281.     int spr;
  2282.     for(spr = 7; spr >= 0; spr--) {
  2283.         if (bpl_info.sprarmed[spr] && currhp == (bpl_info.sprpos[spr] & 0xFF)) {
  2284.         int sprxp = xpos + (bpl_info.sprctl[spr] & 1) * (use_lores ? 1 : 2);
  2285.         pfield_sprite (spr, sprxp, bpl_info.sprdata[spr], bpl_info.sprdatb[spr], use_lores);
  2286.         }
  2287.     }
  2288.     }    
  2289. }
  2290.  
  2291. static int first_drawn_line, last_drawn_line;
  2292. static int first_block_line, last_block_line;
  2293.  
  2294. static void init_frame (void)
  2295. {
  2296.     int i;
  2297.     int maxpos = use_lores ? 400 : 800;
  2298.     int old_pmds = prev_max_diwstop;
  2299.     
  2300.     if (max_diwstop == 0)
  2301.     max_diwstop = diwlastword;
  2302.     
  2303.     if (max_diwstop < (use_lores ? 320 : 640) || max_diwstop > maxpos)
  2304.     prev_max_diwstop = maxpos;
  2305.     else
  2306.     prev_max_diwstop = max_diwstop;
  2307.  
  2308.     max_diwstop = 0;
  2309.  
  2310.     memset(spron, 0, sizeof spron);
  2311.     memset(bpl_info.sprpos, 0, sizeof bpl_info.sprpos);
  2312.     memset(bpl_info.sprctl, 0, sizeof bpl_info.sprctl);
  2313.     last_drawn_line = 0;
  2314.     first_drawn_line = 32767;
  2315.  
  2316.     first_block_line = last_block_line = -2;
  2317.     calc_adjustment();
  2318.     if (frame_redraw_necessary)
  2319.     frame_redraw_necessary--;
  2320.     
  2321.     if (old_pmds != prev_max_diwstop) {
  2322.     frame_redraw_necessary |= (bpl_info.bplcon0 & 4 ? 2 : 1);
  2323.     }
  2324. }
  2325.  
  2326. /*
  2327.  * A raster line has been built in the graphics buffer. Tell the graphics code
  2328.  * to do anything necessary to display it.
  2329.  */
  2330.  
  2331. static void do_flush_line (int lineno)
  2332. {
  2333.     if (lineno < first_drawn_line)
  2334.     first_drawn_line = lineno;
  2335.     if (lineno > last_drawn_line)
  2336.     last_drawn_line = lineno;
  2337.  
  2338.     if (gfxvidinfo.maxblocklines == 0)
  2339.     flush_line(lineno);
  2340.     else {
  2341.     if ((last_block_line+1) != lineno) {
  2342.         if (first_block_line != -2)
  2343.         flush_block (first_block_line, last_block_line);
  2344.         first_block_line = lineno;
  2345.     }
  2346.     last_block_line = lineno;
  2347.     if (last_block_line - first_block_line >= gfxvidinfo.maxblocklines) {
  2348.         flush_block (first_block_line, last_block_line);
  2349.         first_block_line = last_block_line = -2;
  2350.     }
  2351.     }
  2352. }
  2353.  
  2354. /*
  2355.  * One Amiga frame has been finished. Tell the graphics code about it.
  2356.  * Note that the actual flush_scren() call is a no-op for all reasonable
  2357.  * systems.
  2358.  */
  2359.  
  2360. static void do_flush_screen (int start, int stop)
  2361. {
  2362.     if (gfxvidinfo.maxblocklines != 0 && first_block_line != -2) {
  2363.     flush_block (first_block_line, last_block_line);
  2364.     }
  2365.     if (start <= stop)
  2366.     flush_screen (start, stop);
  2367. }
  2368.  
  2369. static void setdontcare(void)
  2370. {
  2371.     fprintf(stderr, "Don't care mouse mode set\n");
  2372.     mousestate = dont_care_mouse;
  2373.     lastspr0x = lastmx; lastspr0y = lastmy;
  2374.     mstepx = defstepx; mstepy = defstepy;
  2375. }
  2376.  
  2377. static void setfollow(void)
  2378. {
  2379.     fprintf(stderr, "Follow sprite mode set\n");
  2380.     mousestate = follow_mouse;
  2381.     lastdiffx = lastdiffy = 0;
  2382.     sprvbfl = 0;
  2383.     spr0ctl=spr0pos = 0;
  2384.     mstepx = defstepx; mstepy = defstepy;
  2385. }
  2386.  
  2387. void togglemouse(void)
  2388. {
  2389.     switch(mousestate) {
  2390.      case dont_care_mouse: setfollow(); break;
  2391.      case follow_mouse: setdontcare(); break;
  2392.      default: break; /* Nnnnnghh! */
  2393.     }
  2394. }        
  2395.  
  2396. static __inline__ int adjust(int val)
  2397. {
  2398.     if (val>127)
  2399.     return 127; 
  2400.     else if (val<-127)
  2401.     return -127;
  2402.     return val;
  2403. }
  2404.  
  2405. static void do_mouse_hack(void)
  2406. {
  2407.     int spr0x = ((spr0pos & 0xff) << 2) | ((spr0ctl & 1) << 1);
  2408.     int spr0y = ((spr0pos >> 8) | ((spr0ctl & 4) << 6)) << 1;
  2409.     int diffx, diffy;
  2410.     
  2411.     switch (mousestate) {
  2412.      case normal_mouse:
  2413.     diffx = lastmx - lastsampledmx;
  2414.     diffy = lastmy - lastsampledmy;
  2415.     if (!newmousecounters) {    
  2416.         if (diffx > 127) diffx = 127;
  2417.         if (diffx < -127) diffx = -127;
  2418.         joy0x += diffx;
  2419.         if (diffy > 127) diffy = 127;
  2420.         if (diffy < -127) diffy = -127;
  2421.         joy0y += diffy;
  2422.     }
  2423.     lastsampledmx += diffx; lastsampledmy += diffy;
  2424.     break;
  2425.  
  2426.      case dont_care_mouse:
  2427.     diffx = adjust (((lastmx-lastspr0x) * mstepx) >> 16);
  2428.     diffy = adjust (((lastmy-lastspr0y) * mstepy) >> 16);
  2429.     lastspr0x=lastmx; lastspr0y=lastmy;
  2430.     joy0x+=diffx; joy0y+=diffy;
  2431.     break;
  2432.     
  2433.      case follow_mouse:
  2434.     if (sprvbfl && sprvbfl-- >1) {
  2435.         int mousexpos, mouseypos;
  2436.         
  2437.         if ((lastdiffx > docal || lastdiffx < -docal) && lastspr0x != spr0x 
  2438.         && spr0x > plfstrt*4 + 34 + xcaloff && spr0x < plfstop*4 - xcaloff)
  2439.         {  
  2440.         int val = (lastdiffx << 16) / (spr0x - lastspr0x);
  2441.         if (val>=0x8000) mstepx=(mstepx*(calweight-1)+val)/calweight;
  2442.         }
  2443.         if ((lastdiffy > docal || lastdiffy < -docal) && lastspr0y != spr0y
  2444.         && spr0y>plffirstline+ycaloff && spr0y<plflastline-ycaloff) 
  2445.         { 
  2446.         int val = (lastdiffy<<16) / (spr0y-lastspr0y);
  2447.         if (val>=0x8000) mstepy=(mstepy*(calweight-1)+val)/calweight;
  2448.         }
  2449.         mousexpos = lastmx;
  2450.         if (gfxvidinfo.x_adjust)
  2451.         mousexpos += gfxvidinfo.x_adjust;
  2452.         mouseypos = lastmy;
  2453.         
  2454.         if(!correct_aspect)
  2455.             mouseypos *= 2;
  2456.         if(use_lores)
  2457.             mousexpos *= 2;
  2458.         if (gfxvidinfo.x_adjust)
  2459.         mousexpos -= 16;
  2460.  
  2461.         diffx = adjust ((((mousexpos + 0x70 + xoffs - spr0x) & ~1) * mstepx) >> 16);
  2462.         diffy = adjust ((((mouseypos + yoffs - spr0y+minfirstline*2) & ~1) * mstepy) >> 16);
  2463.         lastspr0x=spr0x; lastspr0y=spr0y;
  2464.         lastdiffx=diffx; lastdiffy=diffy;
  2465.         joy0x+=diffx; joy0y+=diffy; 
  2466.     }
  2467.     break;
  2468.     }
  2469. }
  2470.  
  2471. static void vsync_handler(void)
  2472. {
  2473.     UWORD dir;
  2474.     int button;
  2475.  
  2476.     handle_events();
  2477.     getjoystate(&joy0dir, &joy0button);
  2478.  
  2479.     do_mouse_hack();
  2480.     
  2481.     INTREQ(0x8020);
  2482.     if (bpl_info.bplcon0 & 4) lof ^= 0x8000;
  2483.     COPJMP1(0);
  2484.     
  2485.     if (framecnt == 0)
  2486.         do_flush_screen (first_drawn_line, last_drawn_line);
  2487.  
  2488.     count_frame();
  2489.     init_frame();
  2490. #ifdef HAVE_GETTIMEOFDAY
  2491.     {
  2492.     struct timeval tv;
  2493.     unsigned long int newtime;
  2494.     
  2495.     gettimeofday(&tv,NULL);    
  2496.     newtime = (tv.tv_sec-seconds_base) * 1000 + tv.tv_usec / 1000;
  2497.     
  2498.     if (!bogusframe) {    
  2499.         frametime += newtime - msecs;
  2500.         timeframes++;
  2501.     }
  2502.     msecs = newtime;
  2503.     bogusframe = 0;
  2504.     }
  2505. #endif
  2506.     CIA_vsync_handler();
  2507. }
  2508.  
  2509. static void hsync_handler(void)
  2510. {
  2511.     int lineno = vpos - minfirstline;
  2512.     int lineisdouble = 0;
  2513.     int line_was_doubled = 0;
  2514.     
  2515.     do_sprites(vpos, maxhpos);
  2516.     last_sprite = 0;
  2517.     
  2518.     if (correct_aspect) {
  2519.     lineno *= 2;
  2520.     if (bpl_info.bplcon0 & 4) {
  2521.         if(!lof) {
  2522.         lineno++;
  2523.         }
  2524.     } else {
  2525.         lineisdouble = 1;
  2526.     }
  2527.     }
  2528.  
  2529.     eventtab[ev_hsync].evtime += cycles - eventtab[ev_hsync].oldcycles;
  2530.     eventtab[ev_hsync].oldcycles = cycles;
  2531.     CIA_hsync_handler();
  2532.     
  2533.     if (produce_sound > 0) {
  2534.     int nr;
  2535.     /* Sound data is fetched at the beginning of each line */
  2536.     for (nr = 0; nr < 4; nr++) {
  2537.         struct audio_channel_data *cdp = audio_channel + nr;
  2538.         
  2539.         if (cdp->data_written == 2) {
  2540.         cdp->data_written = 0;
  2541.         cdp->nextdat = chipmem_bank.wget(cdp->pt);
  2542.         cdp->pt += 2;
  2543.         if (cdp->state == 2 || cdp->state == 3) {
  2544.             if (cdp->wlen == 1) {
  2545.             cdp->pt = cdp->lc;
  2546.             cdp->wlen = cdp->len;
  2547.             cdp->intreq2 = 1;
  2548.             } else
  2549.             cdp->wlen--;
  2550.         }
  2551.         }
  2552.     }
  2553.     }
  2554.  
  2555.     if (framecnt == 0 && vpos >= minfirstline && lineno < gfxvidinfo.maxline) {
  2556.     if (vpos >= plffirstline && vpos < plflastline)
  2557.     {
  2558.         /* Finish the line, if we started doing it with the slow update.
  2559.          * Otherwise, draw it entirely. */
  2560.         if (pfield_fullline) {        
  2561.         if (!pfield_linedone) {
  2562.             /* This can turn on line_in_border if DMA is off */
  2563.             pfield_doline();
  2564.         }
  2565.         if (linetoscreen)
  2566.             pfield_do_linetoscr_full (line_was_doubled = lineisdouble);
  2567.         } else {
  2568.         int i;
  2569.         for(i = pfield_lastpart_hpos; i < maxhpos; i++)
  2570.             pfield_doline_slow(i);
  2571.         pfield_do_linetoscr(slowline_lasttoscr, maxhpos);
  2572.         /* The COLOR routine masks off the high nibble. This means
  2573.          * that there will never be 0xFFFF in color_regs[0], and this
  2574.          * means that the line will be drawn completely the next time
  2575.          * we get into pfield_doline()
  2576.          */
  2577.         linedescr[lineno].bpl_info.color_regs[0] = 0xFFFF;
  2578.         linetoscreen = 1;
  2579.         }
  2580.     } else
  2581.         line_in_border = 1;
  2582.     
  2583.     if (line_in_border && 
  2584.         (!linedescr[lineno].inborder 
  2585.          || linedescr[lineno].bordercol != acolors[0]))
  2586.     {
  2587.         linedescr[lineno].bordercol = acolors[0];
  2588.         linedescr[lineno].linedata_valid = 0;
  2589.         
  2590.         fill_line (lineno);
  2591.         linetoscreen = 1;
  2592.     }
  2593.         
  2594.     linedescr[lineno].inborder = line_in_border;
  2595.     
  2596.     if (linetoscreen) {
  2597. #if AGA_CHIPSET == 1
  2598.         switch (gfxvidinfo.pixbytes) {
  2599.          case 1: aga_translate8 (0, aga_lbufptr-aga_linebuf); break;
  2600.          case 2: aga_translate16 (0, aga_lbufptr-aga_linebuf); break;
  2601.          case 4: aga_translate32 (0, aga_lbufptr-aga_linebuf); break;
  2602.         }
  2603. #endif
  2604.         do_flush_line (lineno);
  2605.     }
  2606.     if (lineisdouble) {
  2607.         int drawit = 0;
  2608.         
  2609.         if (linedescr[lineno].inborder != linedescr[lineno+1].inborder
  2610.         || (linedescr[lineno].inborder == 1
  2611.             && linedescr[lineno].bordercol != linedescr[lineno+1].bordercol)) {
  2612.         drawit = 1;
  2613.         
  2614.         } else if (linedescr[lineno].inborder == 0 
  2615.                && (linetoscreen 
  2616.                || (bplinfo_differs(&linedescr[lineno].bpl_info, 
  2617.                            &linedescr[lineno+1].bpl_info)))) {
  2618.         drawit = 1;
  2619.         }
  2620.         
  2621.         if (drawit) {
  2622.         linedescr[lineno+1].inborder = linedescr[lineno].inborder;
  2623.         linedescr[lineno+1].bordercol = linedescr[lineno].bordercol;
  2624. #if SMART_UPDATE != 0
  2625.         linedescr[lineno+1].bpl_info = linedescr[lineno].bpl_info;
  2626. #endif
  2627.         if (!line_was_doubled) {
  2628.             if (line_in_border)
  2629.             fill_line (lineno+1);
  2630.             else
  2631.             memcpy (gfxvidinfo.bufmem + (lineno+1)*gfxvidinfo.rowbytes,
  2632.                 gfxvidinfo.bufmem + lineno*gfxvidinfo.rowbytes,
  2633.                 gfxvidinfo.rowbytes);
  2634.         }
  2635.         do_flush_line (lineno+1);
  2636.         }
  2637.     }
  2638.     }
  2639.  
  2640.     pfield_calclinedma();
  2641.  
  2642.     if (++vpos == (maxvpos + (lof != 0))) {
  2643.     vpos = 0;
  2644.     vsync_handler();
  2645.     }
  2646.     
  2647.     if (framecnt == 0) 
  2648.     {
  2649.     lineno = vpos - minfirstline;
  2650.     
  2651.     if (correct_aspect) {
  2652.         lineno *= 2;
  2653.         if ((bpl_info.bplcon0 & 4) && !lof) {
  2654.         lineno++;
  2655.         }
  2656.     }
  2657.     xlinebuffer = gfxvidinfo.bufmem + gfxvidinfo.rowbytes * lineno;
  2658.     aga_lbufptr = aga_linebuf;
  2659.     next_lineno = lineno;
  2660.     linetoscreen = 0;
  2661.     line_in_border = 0;
  2662.     pfield_fullline = 1;
  2663.     pfield_linedone = 0;
  2664.     pfield_lastpart_hpos = 0;
  2665.     }
  2666.  
  2667. }
  2668.  
  2669. void customreset(void)
  2670. {
  2671.     int i;
  2672. #ifdef HAVE_GETTIMEOFDAY
  2673.     struct timeval tv;
  2674. #endif
  2675.     inhibit_frame = 0;
  2676.     expamem_reset();
  2677.     CIA_reset();
  2678.     cycles = 0; 
  2679.     regs.spcflags &= SPCFLAG_BRK;
  2680.     
  2681.     last_sprite = 0;
  2682.     vpos = 0; 
  2683.     lof = 0;
  2684.     next_lineno = 0;
  2685.     max_diwstop = 0;
  2686.     
  2687.     if (needmousehack()) {
  2688.         if (mousestate != follow_mouse) setfollow();
  2689.     } else {
  2690.     mousestate = normal_mouse;
  2691.     }
  2692.  
  2693.     memset(spixstate, 0, sizeof(spixstate));
  2694.     
  2695.     /*memset(blitcount, 0, sizeof(blitcount));  blitter debug */
  2696.     
  2697.     for (i = 0; i < numscrlines*2; i++) {
  2698.     linedescr[i].mnn = NULL;
  2699.     linedescr[i].linedata_valid = 0;
  2700.     linedescr[i].bpl_info.color_regs[0] = 0xFFFF;
  2701.     linedescr[i].bplpt[0] = (CPTR)-1;
  2702.     }
  2703.     
  2704.     xlinebuffer = gfxvidinfo.bufmem;
  2705.  
  2706.     dmacon = intena = 0;
  2707.     bltstate = BLT_done;
  2708.     copstate = COP_stop;
  2709.     copcon = 0;
  2710.     dskdmaen = 0;
  2711.     cycles = 0;
  2712.     
  2713.     memset(audio_channel, 0, sizeof audio_channel);
  2714.  
  2715.     bpl_info.bplcon4 = 0x11; /* Get AGA chipset into ECS compatibility mode */
  2716.     bpl_info.bplcon3 = 0xC00;
  2717.     for(i = 0; i < ev_max; i++) {
  2718.     eventtab[i].active = 0;
  2719.     eventtab[i].oldcycles = 0;
  2720.     }
  2721.     copper_active = 0;
  2722.     eventtab[ev_cia].handler = CIA_handler;
  2723.     eventtab[ev_copper].handler = do_copper;
  2724.     eventtab[ev_hsync].handler = hsync_handler;
  2725.     eventtab[ev_hsync].evtime = maxhpos + cycles;
  2726.     eventtab[ev_hsync].active = 1;
  2727.  
  2728.     eventtab[ev_blitter].handler = blitter_handler;
  2729.     eventtab[ev_blitter].active = 0;
  2730.     eventtab[ev_diskblk].handler = diskblk_handler;
  2731.     eventtab[ev_diskblk].active = 0;
  2732.     eventtab[ev_diskindex].handler = diskindex_handler;
  2733.     eventtab[ev_diskindex].active = 0;
  2734. #ifndef DONT_WANT_SOUND
  2735.     eventtab[ev_aud0].handler = aud0_handler;
  2736.     eventtab[ev_aud0].active = 0;
  2737.     eventtab[ev_aud1].handler = aud1_handler;
  2738.     eventtab[ev_aud1].active = 0;
  2739.     eventtab[ev_aud2].handler = aud2_handler;
  2740.     eventtab[ev_aud2].active = 0;
  2741.     eventtab[ev_aud3].handler = aud3_handler;
  2742.     eventtab[ev_aud3].active = 0;
  2743.     if (sound_available) {
  2744.     eventtab[ev_sample].active = 1;
  2745.     eventtab[ev_sample].evtime += cycles;
  2746.     eventtab[ev_sample].oldcycles = cycles;
  2747.     } else {
  2748.     eventtab[ev_sample].active = 0;
  2749.     }
  2750. #endif
  2751.     events_schedule();
  2752.     
  2753.     init_frame();
  2754. #ifdef HAVE_GETTIMEOFDAY
  2755.     gettimeofday(&tv,NULL);
  2756.     seconds_base = tv.tv_sec;
  2757.     bogusframe = 1;
  2758. #endif
  2759. }
  2760.  
  2761. void dumpcustom(void)
  2762. {
  2763.     int i;
  2764.     fprintf(stderr, "DMACON: %x INTENA: %x INTREQ: %x VPOS: %x HPOS: %x\n", DMACONR(),
  2765.        intena, intreq, vpos, current_hpos());
  2766.     if (timeframes) { 
  2767.     fprintf(stderr, "Average frame time: %d ms [frames: %d time: %d]\n", 
  2768.            frametime/timeframes, timeframes, frametime);
  2769.     }
  2770.     /*for (i=0; i<256; i++) if (blitcount[i]) fprintf(stderr, "minterm %x = %d\n",i,blitcount[i]);  blitter debug */
  2771. }
  2772.  
  2773. int intlev(void)
  2774. {
  2775.     UWORD imask = intreq & intena;
  2776.     if (imask && (intena & 0x4000)){
  2777.     if (imask & 0x2000) return 6;
  2778.     if (imask & 0x1800) return 5;
  2779.     if (imask & 0x0780) return 4;
  2780.     if (imask & 0x0070) return 3;
  2781.     if (imask & 0x0008) return 2;
  2782.     if (imask & 0x0007) return 1;
  2783.     }
  2784.     return -1;
  2785. }
  2786.  
  2787. void custom_init(void)
  2788. {
  2789.     int num;
  2790.     if (needmousehack())
  2791.     setfollow();
  2792.     customreset();
  2793.     for (num = 0; num < 256; num++) {    
  2794.     int plane1 = (num & 1) | ((num >> 1) & 2) | ((num >> 2) & 4) | ((num >> 3) & 8);
  2795.     int plane2 = ((num >> 1) & 1) | ((num >> 2) & 2) | ((num >> 3) & 4) | ((num >> 4) & 8);
  2796.     dblpf_2nd1[num] = plane1 == 0 ? (plane2 == 0 ? 0 : 2) : 1;
  2797.     dblpf_2nd2[num] = plane2 == 0 ? (plane1 == 0 ? 0 : 1) : 2;
  2798.     dblpf_aga1[num] = plane1 == 0 ? plane2 : plane1;
  2799.     dblpf_aga2[num] = plane2 == 0 ? plane1 : plane2;
  2800.     if (plane2 > 0) plane2 += 8;
  2801.     dblpf_ind1[num] = plane1 == 0 ? plane2 : plane1;
  2802.     dblpf_ind2[num] = plane2 == 0 ? plane1 : plane2;
  2803.     
  2804.     lots_of_twos[num] = num == 0 ? 0 : 2;
  2805.     linear_map_256[num] = num;
  2806.     }
  2807.     build_blitfilltable();
  2808.     gen_pfield_tables();
  2809. }
  2810.  
  2811. /* Custom chip memory bank */
  2812.  
  2813. static ULONG custom_lget(CPTR) REGPARAM;
  2814. static UWORD custom_wget(CPTR) REGPARAM;
  2815. static UBYTE custom_bget(CPTR) REGPARAM;
  2816. static void  custom_lput(CPTR, ULONG) REGPARAM;
  2817. static void  custom_wput(CPTR, UWORD) REGPARAM;
  2818. static void  custom_bput(CPTR, UBYTE) REGPARAM;
  2819.  
  2820. addrbank custom_bank = {
  2821.     default_alget, default_awget,
  2822.     custom_lget, custom_wget, custom_bget,
  2823.     custom_lput, custom_wput, custom_bput,
  2824.     default_xlate, default_check
  2825. };
  2826.  
  2827. UWORD custom_wget(CPTR addr)
  2828. {
  2829.     switch(addr & 0x1FE) {
  2830.      case 0x002: return DMACONR();
  2831.      case 0x004: return VPOSR();
  2832.      case 0x006: return VHPOSR();
  2833.     
  2834.      case 0x008: return DSKDATR();
  2835.  
  2836.      case 0x00A: return JOY0DAT();
  2837.      case 0x00C: return JOY1DAT();
  2838.      case 0x010: return ADKCONR();
  2839.  
  2840.      case 0x012: return POT0DAT();
  2841.      case 0x016: return POTGOR();
  2842.      case 0x018: return SERDATR();
  2843.      case 0x01A: return DSKBYTR();
  2844.      case 0x01C: return INTENAR();
  2845.      case 0x01E: return INTREQR();
  2846.  
  2847. #if AGA_CHIPSET == 1
  2848.      case 0x07C: return 0xF8;
  2849. #elif defined ECS_DENISE
  2850.      case 0x07C: return 0xFC;
  2851. #endif
  2852.      default:
  2853.     custom_wput(addr,0);
  2854.     return 0xffff;
  2855.     }
  2856. }
  2857.  
  2858. UBYTE custom_bget(CPTR addr)
  2859. {
  2860.     return custom_wget(addr & 0xfffe) >> (addr & 1 ? 0 : 8);
  2861. }
  2862.  
  2863. ULONG custom_lget(CPTR addr)
  2864. {
  2865.     return ((ULONG)custom_wget(addr & 0xfffe) << 16) | custom_wget((addr+2) & 0xfffe);
  2866. }
  2867.  
  2868. void custom_wput(CPTR addr, UWORD value)
  2869. {
  2870.     addr &= 0x1FE;
  2871.     cregs[addr>>1] = value;
  2872.     switch(addr) {    
  2873.      case 0x020: DSKPTH(value); break;
  2874.      case 0x022: DSKPTL(value); break;
  2875.      case 0x024: DSKLEN(value); break;
  2876.      case 0x026: DSKDAT(value); break;
  2877.     
  2878.      case 0x02A: VPOSW(value); break;
  2879.  
  2880.      case 0x030: SERDAT(value); break;
  2881.      case 0x032: SERPER(value); break;
  2882.  
  2883.      case 0x040: BLTCON0(value); break;
  2884.      case 0x042: BLTCON1(value); break;
  2885.     
  2886.      case 0x044: BLTAFWM(value); break;
  2887.      case 0x046: BLTALWM(value); break;
  2888.     
  2889.      case 0x050: BLTAPTH(value); break;
  2890.      case 0x052: BLTAPTL(value); break;
  2891.      case 0x04C: BLTBPTH(value); break;
  2892.      case 0x04E: BLTBPTL(value); break;
  2893.      case 0x048: BLTCPTH(value); break;
  2894.      case 0x04A: BLTCPTL(value); break;
  2895.      case 0x054: BLTDPTH(value); break;
  2896.      case 0x056: BLTDPTL(value); break;
  2897.     
  2898.      case 0x058: BLTSIZE(value); break;
  2899.     
  2900.      case 0x064: BLTAMOD(value); break;
  2901.      case 0x062: BLTBMOD(value); break;
  2902.      case 0x060: BLTCMOD(value); break;
  2903.      case 0x066: BLTDMOD(value); break;
  2904.     
  2905.      case 0x070: BLTCDAT(value); break;
  2906.      case 0x072: BLTBDAT(value); break;
  2907.      case 0x074: BLTADAT(value); break;
  2908.             
  2909.      case 0x07E: DSKSYNC(value); break;
  2910.  
  2911.      case 0x080: COP1LCH(value); break;
  2912.      case 0x082: COP1LCL(value); break;
  2913.      case 0x084: COP2LCH(value); break;
  2914.      case 0x086: COP2LCL(value); break;
  2915.     
  2916.      case 0x088: COPJMP1(value); break;
  2917.      case 0x08A: COPJMP2(value); break;
  2918.     
  2919.      case 0x08E: DIWSTRT(value); break;
  2920.      case 0x090: DIWSTOP(value); break;
  2921.      case 0x092: DDFSTRT(value); break;
  2922.      case 0x094: DDFSTOP(value); break;
  2923.     
  2924.      case 0x096: DMACON(value); break;
  2925.      case 0x09A: INTENA(value); break;
  2926.      case 0x09C: INTREQ(value); break;
  2927.      case 0x09E: ADKCON(value); break;
  2928.     
  2929.      case 0x0A0: AUDxLCH(0, value); break;
  2930.      case 0x0A2: AUDxLCL(0, value); break;
  2931.      case 0x0A4: AUDxLEN(0, value); break;
  2932.      case 0x0A6: AUDxPER(0, value); break;
  2933.      case 0x0A8: AUDxVOL(0, value); break;
  2934.      case 0x0AA: AUDxDAT(0, value); break;
  2935.     
  2936.      case 0x0B0: AUDxLCH(1, value); break;
  2937.      case 0x0B2: AUDxLCL(1, value); break;
  2938.      case 0x0B4: AUDxLEN(1, value); break;
  2939.      case 0x0B6: AUDxPER(1, value); break;
  2940.      case 0x0B8: AUDxVOL(1, value); break;
  2941.      case 0x0BA: AUDxDAT(1, value); break;
  2942.     
  2943.      case 0x0C0: AUDxLCH(2, value); break;
  2944.      case 0x0C2: AUDxLCL(2, value); break;
  2945.      case 0x0C4: AUDxLEN(2, value); break;
  2946.      case 0x0C6: AUDxPER(2, value); break;
  2947.      case 0x0C8: AUDxVOL(2, value); break;
  2948.      case 0x0CA: AUDxDAT(2, value); break;
  2949.     
  2950.      case 0x0D0: AUDxLCH(3, value); break;
  2951.      case 0x0D2: AUDxLCL(3, value); break;
  2952.      case 0x0D4: AUDxLEN(3, value); break;
  2953.      case 0x0D6: AUDxPER(3, value); break;
  2954.      case 0x0D8: AUDxVOL(3, value); break;
  2955.      case 0x0DA: AUDxDAT(3, value); break;
  2956.     
  2957.      case 0x0E0: BPLPTH(value, 0); break;
  2958.      case 0x0E2: BPLPTL(value, 0); break;
  2959.      case 0x0E4: BPLPTH(value, 1); break;
  2960.      case 0x0E6: BPLPTL(value, 1); break;
  2961.      case 0x0E8: BPLPTH(value, 2); break;
  2962.      case 0x0EA: BPLPTL(value, 2); break;
  2963.      case 0x0EC: BPLPTH(value, 3); break;
  2964.      case 0x0EE: BPLPTL(value, 3); break;
  2965.      case 0x0F0: BPLPTH(value, 4); break;
  2966.      case 0x0F2: BPLPTL(value, 4); break;
  2967.      case 0x0F4: BPLPTH(value, 5); break;
  2968.      case 0x0F6: BPLPTL(value, 5); break;
  2969.     
  2970.      case 0x100: BPLCON0(value); break;
  2971.      case 0x102: BPLCON1(value); break;
  2972.      case 0x104: BPLCON2(value); break;
  2973.      case 0x106: BPLCON3(value); break;
  2974.     
  2975.      case 0x108: BPL1MOD(value); break;
  2976.      case 0x10A: BPL2MOD(value); break;
  2977.  
  2978.      case 0x110: BPL1DAT(value); break;
  2979.      case 0x112: BPL2DAT(value); break;
  2980.      case 0x114: BPL3DAT(value); break;
  2981.      case 0x116: BPL4DAT(value); break;
  2982.      case 0x118: BPL5DAT(value); break;
  2983.      case 0x11A: BPL6DAT(value); break;
  2984.     
  2985.      case 0x180: case 0x182: case 0x184: case 0x186: case 0x188: case 0x18A:
  2986.      case 0x18C: case 0x18E: case 0x190: case 0x192: case 0x194: case 0x196:
  2987.      case 0x198: case 0x19A: case 0x19C: case 0x19E: case 0x1A0: case 0x1A2:
  2988.      case 0x1A4: case 0x1A6: case 0x1A8: case 0x1AA: case 0x1AC: case 0x1AE:
  2989.      case 0x1B0: case 0x1B2: case 0x1B4: case 0x1B6: case 0x1B8: case 0x1BA:
  2990.      case 0x1BC: case 0x1BE: 
  2991.     COLOR(value & 0xFFF, (addr & 0x3E) / 2);
  2992.     break;    
  2993.      case 0x120: case 0x124: case 0x128: case 0x12C: 
  2994.      case 0x130: case 0x134: case 0x138: case 0x13C:
  2995.     SPRxPTH(value, (addr - 0x120) / 4);
  2996.     break;
  2997.      case 0x122: case 0x126: case 0x12A: case 0x12E: 
  2998.      case 0x132: case 0x136: case 0x13A: case 0x13E:
  2999.     SPRxPTL(value, (addr - 0x122) / 4);
  3000.     break;
  3001.      case 0x140: case 0x148: case 0x150: case 0x158: 
  3002.      case 0x160: case 0x168: case 0x170: case 0x178:
  3003.     SPRxPOS(value, (addr - 0x140) / 8);
  3004.     break;
  3005.      case 0x142: case 0x14A: case 0x152: case 0x15A: 
  3006.      case 0x162: case 0x16A: case 0x172: case 0x17A:
  3007.     SPRxCTL(value, (addr - 0x142) / 8);
  3008.     break;
  3009.      case 0x144: case 0x14C: case 0x154: case 0x15C:
  3010.      case 0x164: case 0x16C: case 0x174: case 0x17C:
  3011.     SPRxDATA(value, (addr - 0x144) / 8);
  3012.     break;
  3013.      case 0x146: case 0x14E: case 0x156: case 0x15E: 
  3014.      case 0x166: case 0x16E: case 0x176: case 0x17E:
  3015.     SPRxDATB(value, (addr - 0x146) / 8);
  3016.     break;
  3017.     
  3018.      case 0x36: JOYTEST(value); break;
  3019. #if defined(ECS_AGNUS) || (AGA_CHIPSET == 1)
  3020.      case 0x5A: BLTCON0L(value); break;
  3021.      case 0x5C: BLTSIZV(value); break;
  3022.      case 0x5E: BLTSIZH(value); break;
  3023. #endif
  3024. #if AGA_CHIPSET == 1
  3025.      case 0x10C: BPLCON4(value); break;
  3026.      case 0x1FC: fmode = value; break;
  3027. #endif
  3028.     }
  3029. }
  3030.  
  3031. void custom_bput(CPTR addr, UBYTE value)
  3032. {
  3033.     /* Yes, there are programs that do this. The programmers should be shot.
  3034.      * This might actually work sometimes. */
  3035.     UWORD rval = value;
  3036.     CPTR raddr = addr & 0x1FE;
  3037.     if (addr & 1) {
  3038.     rval |= cregs[raddr >> 1] & 0xFF00;
  3039.     } else {
  3040.     rval <<= 8;
  3041.     rval |= cregs[raddr >> 1] & 0xFF;
  3042.     }
  3043.     custom_wput(raddr, rval);
  3044. }
  3045.  
  3046. void custom_lput(CPTR addr, ULONG value)
  3047. {
  3048.     custom_wput(addr & 0xfffe, value >> 16);
  3049.     custom_wput((addr+2) & 0xfffe, (UWORD)value);
  3050. }
  3051.  
  3052.